ForeignKey

public abstract @interface ForeignKey
implements Annotation

androidx.room.ForeignKey


Declares a foreign key on another Entity.

Foreign keys allows you to specify constraints across Entities such that SQLite will ensure that the relationship is valid when you modify the database.

When a foreign key constraint is specified, SQLite requires the referenced columns to be part of a unique index in the parent table or the primary key of that table. You must create a unique index in the parent entity that covers the referenced columns (Room will verify this at compile time and print an error if it is missing).

It is also recommended to create an index on the child table to avoid full table scans when the parent table is modified. If a suitable index on the child table is missing, Room will print RoomWarnings.MISSING_INDEX_ON_FOREIGN_KEY_CHILD warning.

A foreign key constraint can be deferred until the transaction is complete. This is useful if you are doing bulk inserts into the database in a single transaction. By default, foreign key constraints are immediate but you can change this value by setting deferred() to true. You can also use defer_foreign_keys PRAGMA to defer them depending on your transaction.

Please refer to the SQLite foreign keys documentation for details.

Summary

Nested classes

@interface ForeignKey.Action

Constants definition for values that can be used in ForeignKey.onDelete() and ForeignKey.onUpdate()

Constants

int CASCADE

Possible value for onDelete() or onUpdate().

int NO_ACTION

Possible value for onDelete() or onUpdate().

int RESTRICT

Possible value for onDelete() or onUpdate().

int SET_DEFAULT

Possible value for onDelete() or onUpdate().

int SET_NULL

Possible value for onDelete() or onUpdate().

Public methods

String[] childColumns()

The list of column names in the current Entity.

boolean deferred()

* A foreign key constraint can be deferred until the transaction is complete.

Class<?> entity()

The parent Entity to reference.

int onDelete()

Action to take when the parent Entity is deleted from the database.

int onUpdate()

Action