ColumnInfo


@Target(allowedTargets = [AnnotationTarget.FIELD, AnnotationTarget.FUNCTION])
@Retention(value = AnnotationRetention.BINARY)
public annotation ColumnInfo


Allows specific customization about the column associated with this field.

For example, you can specify a column name for the field or change the column's type affinity.

Summary

Nested types

@RequiresApi(value = 21)
@IntDef(value = [1, 2, 3, 4, 5, 6])
@Retention(value = AnnotationRetention.BINARY)
public annotation ColumnInfo.Collate
@IntDef(value = [1, 2, 3, 4, 5])
@Retention(value = AnnotationRetention.BINARY)
public annotation ColumnInfo.SQLiteTypeAffinity

The SQLite column type constants that can be used in typeAffinity()

Constants

static final int
BINARY = 2

Collation sequence for case-sensitive match.

static final int
BLOB = 5

Column affinity constant for binary data.

static final @NonNull String

Constant to let Room inherit the field name as the column name.

static final int

Column affinity constant for integers or booleans.

static final int
@RequiresApi(value = 21)
LOCALIZED = 5

Collation sequence that uses system's current locale.

static final int
NOCASE = 3

Collation sequence for case-insensitive match.

static final int
REAL = 4

Column affinity constant for floats or doubles.

static final int
RTRIM = 4

Collation sequence for case-sensitive match except that trailing space characters are ignored.

static final int
TEXT = 2

Column affinity constant for strings.

static final int

Undefined type affinity.

static final int
@RequiresApi(value = 21)
UNICODE = 6

Collation sequence that uses Unicode Collation Algorithm.

static final int

Collation sequence is not specified.

static final @NonNull String

A constant for defaultValue() that makes the column to have no default value.

Public constructors

ColumnInfo(
    @NonNull String name,
    int typeAffinity,
    boolean index,
    int collate,
    @NonNull String defaultValue
)

Public methods

final int

The collation sequence for the column, which will be used when constructing the database.

final @NonNull String

The default value for this column.

final boolean

Convenience method to index the field.

final @NonNull String

Name of the column in the database.

final int

The type affinity for the column, which will be used when constructing the database.

Constants

BINARY

Added in 2.5.0
public static final int BINARY = 2

Collation sequence for case-sensitive match.

See also
collate

()

BLOB

Added in 2.5.0
public static final int BLOB = 5

Column affinity constant for binary data.

See also
typeAffinity

()

INHERIT_FIELD_NAME

Added in 2.5.0
public static final @NonNull String INHERIT_FIELD_NAME

Constant to let Room inherit the field name as the column name. If used, Room will use the field name as the column name.

INTEGER

Added in 2.5.0
public static final int INTEGER = 3

Column affinity constant for integers or booleans.

See also
typeAffinity

()

LOCALIZED

Added in 2.5.0
@RequiresApi(value = 21)
public static final int LOCALIZED = 5

Collation sequence that uses system's current locale.

See also
collate

()

NOCASE

Added in 2.5.0
public static final int NOCASE = 3

Collation sequence for case-insensitive match.

See also
collate

()

REAL

Added in 2.5.0
public static final int REAL = 4

Column affinity constant for floats or doubles.

See also
typeAffinity

()

RTRIM

Added in 2.5.0
public static final int RTRIM = 4

Collation sequence for case-sensitive match except that trailing space characters are ignored.

See also
collate

()

TEXT

Added in 2.5.0
public static final int TEXT = 2

Column affinity constant for strings.

See also
typeAffinity

()

UNDEFINED

Added in 2.5.0
public static final int UNDEFINED = 1

Undefined type affinity. Will be resolved based on the type.

See also
typeAffinity

()

UNICODE

Added in 2.5.0
@RequiresApi(value = 21)
public static final int UNICODE = 6

Collation sequence that uses Unicode Collation Algorithm.

See also
collate

()

UNSPECIFIED

Added in 2.5.0
public static final int UNSPECIFIED = 1

Collation sequence is not specified. The match will behave like BINARY.

See also
collate

()

VALUE_UNSPECIFIED

Added in 2.5.0
public static final @NonNull String VALUE_UNSPECIFIED

A constant for defaultValue() that makes the column to have no default value.

Public constructors

ColumnInfo

public ColumnInfo(
    @NonNull String name,
    int typeAffinity,
    boolean index,
    int collate,
    @NonNull String defaultValue
)

Public methods

getCollate

@ColumnInfo.Collate
public final int getCollate()

The collation sequence for the column, which will be used when constructing the database.

The default value is UNSPECIFIED. In that case, Room does not add any collation sequence to the column, and SQLite treats it like BINARY.

Returns
int

The collation sequence of the column. This is either UNSPECIFIED, BINARY, NOCASE, RTRIM, LOCALIZED or UNICODE.

getDefaultValue

public final @NonNull String getDefaultValue()

The default value for this column.

@ColumnInfo(defaultValue = "No name")
public name: String

@ColumnInfo(defaultValue = "0")
public flag: Int

Note that the default value you specify here will NOT be used if you simply insert the Entity with Insert. In that case, any value assigned in Java/Kotlin will be used. Use Query with an INSERT statement and skip this column there in order to use this default value.

NULL, CURRENT_TIMESTAMP and other SQLite constant values are interpreted as such. If you want to use them as strings for some reason, surround them with single-quotes.

@ColumnInfo(defaultValue = "NULL")
public description: String?

@ColumnInfo(defaultValue = "'NULL'")
public name: String

You can also use constant expressions by surrounding them with parentheses.

@ColumnInfo(defaultValue = "('Created at' || CURRENT_TIMESTAMP)")
public notice: String
Returns
@NonNull String

The default value for this column.

getIndex

public final boolean getIndex()

Convenience method to index the field.

If you would like to create a composite index instead, see: Index.

Returns
boolean

True if this field should be indexed, false otherwise. Defaults to false.

getName

public final @NonNull String getName()

Name of the column in the database. Defaults to the field name if not set.

Returns
@NonNull String

Name of the column in the database.

getTypeAffinity

@ColumnInfo.SQLiteTypeAffinity
public final int getTypeAffinity()

The type affinity for the column, which will be used when constructing the database.

If it is not specified, the value defaults to UNDEFINED and Room resolves it based on the field's type and available TypeConverters.

See SQLite types documentation for details.

Returns
int

The type affinity of the column. This is either UNDEFINED, TEXT, INTEGER, REAL, or BLOB.