PrimaryKey


@Target(allowedTargets = [AnnotationTarget.FIELDAnnotationTarget.FUNCTION])
@Retention(value = AnnotationRetention.BINARY)
public annotation PrimaryKey


Marks a property in an Entity as the primary key.

If you would like to define a composite primary key, you should use Entity.primaryKeys function.

Each Entity must declare a primary key unless one of its super classes declares a primary key. If both an Entity and its super class defines a PrimaryKey, the child's PrimaryKey definition will override the parent's PrimaryKey.

If this annotation is used on a Embedded property, all columns inherited from that embedded property becomes the composite primary key (including its grand children properties).

Summary

Nested types

public enum PrimaryKey.Algorithm extends Enum

The PrimaryKey auto-generate algorithms.

Public constructors

PrimaryKey(boolean autoGenerate, @NonNull PrimaryKey.Algorithm algorithm)

Public methods

final @NonNull PrimaryKey.Algorithm

When autoGenerate is set to true then the algorithm used to auto-generate primary keys can be configured through this property.

final boolean

Set to true to let SQLite generate the unique id.

Public constructors

PrimaryKey

Added in 3.0.0-rc01
public PrimaryKey(boolean autoGenerate, @NonNull PrimaryKey.Algorithm algorithm)

Public methods

getAlgorithm

public final @NonNull PrimaryKey.Algorithm getAlgorithm()

When autoGenerate is set to true then the algorithm used to auto-generate primary keys can be configured through this property. When autoGenerate is set to false this value is ignored.

For details, see the auto increment documentation.

Returns
@NonNull PrimaryKey.Algorithm

The algorithm to use for auto-generation. Defaults to Algorithm.AUTOINCREMENT.

See also

getAutoGenerate

public final boolean getAutoGenerate()

Set to true to let SQLite generate the unique id.

When set to true, the SQLite type affinity for the property should be INTEGER.

If the property type is Long or Int (or its ColumnTypeConverter converts it to a Long or Int), Insert functions treat 0 as not-set while inserting the item. Similarly, if the property type is nullable (Long? or Int?), Insert functions treat null as not-set while inserting the item.

The strategy on how the id will be generated will be based on the algorithm property.

Returns
boolean

Whether the primary key should be auto-generated by SQLite or not. Defaults to false.

See also