The Android Developer Challenge is back! Submit your idea before December 2.

ColumnInfo

@Target([AnnotationTarget.FIELD, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER]) class ColumnInfo
androidx.room.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 classes

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

Constants

static Int

Collation sequence for case-sensitive match.

static Int

Column affinity constant for binary data.

static String

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

static Int

Column affinity constant for integers or booleans.

static Int

Collation sequence that uses system's current locale.

static Int

Collation sequence for case-insensitive match.

static Int

Column affinity constant for floats or doubles.

static Int

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

static Int

Column affinity constant for strings.

static Int

Undefined type affinity.

static Int

Collation sequence that uses Unicode Collation Algorithm.

static Int

Collation sequence is not specified.

static String

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

Public constructors

<init>(name: String, typeAffinity: Int, index: Boolean, collate: Int, defaultValue: String)

Allows specific customization about the column associated with this field.

Properties

Int

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

String

The default value for this column.

Boolean

Convenience method to index the field.

String

Name of the column in the database.

Int

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

Constants

BINARY

static val BINARY: Int

Collation sequence for case-sensitive match.

Value: 2

See Also

BLOB

static val BLOB: Int

Column affinity constant for binary data.

Value: 5

See Also

INHERIT_FIELD_NAME

static val INHERIT_FIELD_NAME: String

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

Value: "[field-name]"

INTEGER

static val INTEGER: Int

Column affinity constant for integers or booleans.

Value: 3

See Also

LOCALIZED

static val LOCALIZED: Int

Collation sequence that uses system's current locale.

Value: 5

See Also

NOCASE

static val NOCASE: Int

Collation sequence for case-insensitive match.

Value: 3

See Also

REAL

static val REAL: Int

Column affinity constant for floats or doubles.

Value: 4

See Also

RTRIM

static val RTRIM: Int

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

Value: 4

See Also

TEXT

static val TEXT: Int

Column affinity constant for strings.

Value: 2

See Also

UNDEFINED

static val UNDEFINED: Int

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

Value: 1

See Also

UNICODE

static val UNICODE: Int

Collation sequence that uses Unicode Collation Algorithm.

Value: 6

See Also

UNSPECIFIED

static val UNSPECIFIED: Int

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

Value: 1

See Also

VALUE_UNSPECIFIED

static val VALUE_UNSPECIFIED: String

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

Value: "[value-unspecified]"

Public constructors

<init>

ColumnInfo(name: String, typeAffinity: Int, index: Boolean, collate: Int, defaultValue: String)

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.

Properties

collate

val collate: Int

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.

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

defaultValue

val defaultValue: String

The default value for this column.

@ColumnInfo(defaultValue = "No name")
        public String name;
     
       @ColumnInfo(defaultValue = "0")
        public int flag;
      

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")
        @Nullable
        public String description;
     
        @ColumnInfo(defaultValue = "'NULL'")
        @NonNull
        public String name;
      

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

@CoumnInfo(defaultValue = "('Created at' || CURRENT_TIMESTAMP)")
        public String notice;
      
Return
String: The default value for this column.

index

val index: Boolean

Convenience method to index the field.

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

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

name

val name: String

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

Return
String: Name of the column in the database.

typeAffinity

val typeAffinity: Int

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.

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