ColumnInfo

@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.

const Int
BLOB = 5

Column affinity constant for binary data.

const String

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

const Int

Column affinity constant for integers or booleans.

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

Collation sequence that uses system's current locale.

const Int
NOCASE = 3

Collation sequence for case-insensitive match.

const Int
REAL = 4

Column affinity constant for floats or doubles.

const Int
RTRIM = 4

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

const Int
TEXT = 2

Column affinity constant for strings.

const Int

Undefined type affinity.

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

Collation sequence that uses Unicode Collation Algorithm.

const Int

Collation sequence is not specified.

const String

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

Public constructors

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

Public 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

Added in 2.5.0
const val BINARY = 2: Int

Collation sequence for case-sensitive match.

See also
collate

()

BLOB

Added in 2.5.0
const val BLOB = 5: Int

Column affinity constant for binary data.

See also
typeAffinity

()

INHERIT_FIELD_NAME

Added in 2.5.0
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

Added in 2.5.0
const val INTEGER = 3: Int

Column affinity constant for integers or booleans.

See also
typeAffinity

()

LOCALIZED

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

Collation sequence that uses system's current locale.

See also
collate

()

NOCASE

Added in 2.5.0
const val NOCASE = 3: Int

Collation sequence for case-insensitive match.

See also
collate

()

REAL

Added in 2.5.0
const val REAL = 4: Int

Column affinity constant for floats or doubles.

See also
typeAffinity

()

RTRIM

Added in 2.5.0
const val RTRIM = 4: Int

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

See also
collate

()

TEXT

Added in 2.5.0
const val TEXT = 2: Int

Column affinity constant for strings.

See also
typeAffinity

()

UNDEFINED

Added in 2.5.0
const val UNDEFINED = 1: Int

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

See also
typeAffinity

()

UNICODE

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

Collation sequence that uses Unicode Collation Algorithm.

See also
collate

()

UNSPECIFIED

Added in 2.5.0
const val UNSPECIFIED = 1: Int

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

See also
collate

()

VALUE_UNSPECIFIED

Added in 2.5.0
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.