ColumnInfo


  • Cmn
    @Target(allowedTargets = [AnnotationTarget.FIELD, AnnotationTarget.FUNCTION])
    @Retention(value = AnnotationRetention.BINARY)
    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)
annotation ColumnInfo.Collate
@IntDef(value = [1, 2, 3, 4, 5])
@Retention(value = AnnotationRetention.BINARY)
annotation ColumnInfo.SQLiteTypeAffinity

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

Constants

const Int
BINARY = 2

Collation sequence for case-sensitive match.

Cmn
const Int
BLOB = 5

Column affinity constant for binary data.

Cmn
const String

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

Cmn
const Int

Column affinity constant for integers or booleans.

Cmn
const Int
@RequiresApi(value = 21)
LOCALIZED = 5

Collation sequence that uses system's current locale.

Cmn
const Int
NOCASE = 3

Collation sequence for case-insensitive match.

Cmn
const Int
REAL = 4

Column affinity constant for floats or doubles.

Cmn
const Int
RTRIM = 4

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

Cmn
const Int
TEXT = 2

Column affinity constant for strings.

Cmn
const Int

Undefined type affinity.

Cmn
const Int
@RequiresApi(value = 21)
UNICODE = 6

Collation sequence that uses Unicode Collation Algorithm.

Cmn
const Int

Collation sequence is not specified.

Cmn
const String

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

Cmn

Public constructors

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

Public properties

Int

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

Cmn
String

The default value for this column.

Cmn
Boolean

Convenience method to index the field.

Cmn
String

Name of the column in the database.

Cmn
Int

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

Cmn

Constants

BINARY

const val BINARY = 2: Int

Collation sequence for case-sensitive match.

See also
collate

()

BLOB

const val BLOB = 5: Int

Column affinity constant for binary data.

See also
typeAffinity

()

INHERIT_FIELD_NAME

const val INHERIT_FIELD_NAMEString

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

const val INTEGER = 3: Int

Column affinity constant for integers or booleans.

See also
typeAffinity

()

LOCALIZED

@RequiresApi(value = 21)
const val LOCALIZED = 5: Int

Collation sequence that uses system's current locale.

See also
collate

()

NOCASE

const val NOCASE = 3: Int

Collation sequence for case-insensitive match.

See also
collate

()

REAL

const val REAL = 4: Int

Column affinity constant for floats or doubles.

See also
typeAffinity

()

RTRIM

const val RTRIM = 4: Int

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

See also
collate

()

TEXT

const val TEXT = 2: Int

Column affinity constant for strings.

See also
typeAffinity

()

UNDEFINED

const val UNDEFINED = 1: Int

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

See also
typeAffinity

()

UNICODE

@RequiresApi(value = 21)
const val UNICODE = 6: Int

Collation sequence that uses Unicode Collation Algorithm.

See also
collate

()

UNSPECIFIED

const val UNSPECIFIED = 1: Int

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

See also
collate

()

VALUE_UNSPECIFIED

const val VALUE_UNSPECIFIEDString

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

Public constructors

ColumnInfo

ColumnInfo(
    name: String = INHERIT_FIELD_NAME,
    typeAffinity: Int = UNDEFINED,
    index: Boolean = false,
    collate: Int = UNSPECIFIED,
    defaultValue: String = VALUE_UNSPECIFIED
)

Public properties

collate

val collateInt

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.

defaultValue

val defaultValueString

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
String

The default value for this column.

index

val indexBoolean

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.

name

val nameString

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

Returns
String

Name of the column in the database.

typeAffinity

val typeAffinityInt

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.