references method
Adds a foreign-key reference from this column.
The table
type must be a Dart class name defining a drift table.
The column
must be a Dart symbol with the same name as a column in the
referenced table.
In Dart, symbols can be created by prefixing an identifier with #
.
In the following example, a Books
table keeps a reference to the author
of each book:
class Authors extends Table {
IntColumn get id => integer().autoIncrement()();
// ...
}
class Books extends Table {
IntColumn get author => integer().references(Authors, #id)();
}
Important notice: In sqlite3, foreign keys are not enabled by default. When using foreign keys, remember to enable the option in the MigrationStrategy.beforeOpen callback:
beforeOpen: (details) async {
await customStatement('PRAGMA foreign_keys = ON');
}
The optional onUpdate
and onDelete
actions can be used to
automatically propagate changes from the referenced row to this column.
See KeyAction for details.
initiallyDeferred
can be set to true
to generate the foreign key as
DEFERRABLE INITIALLY DEFERRED
, meaning that it is checked at the end of
a transaction only instead of after every statement.
Implementation
ColumnBuilder<T> references(
Type table,
Symbol column, {
KeyAction? onUpdate,
KeyAction? onDelete,
bool initiallyDeferred = false,
}) {
_isGenerated();
}