foreignKey method

SchemaUtils foreignKey(
  1. String referencedTable,
  2. String referencedColumn, {
  3. String? onDelete,
  4. String? onUpdate,
})

Add FOREIGN KEY constraint

Creates a foreign key relationship to another table. referencedTable is the table being referenced. referencedColumn is the column being referenced (usually 'id'). onDelete specifies what happens when the referenced row is deleted. onUpdate specifies what happens when the referenced row is updated.

Implementation

SchemaUtils foreignKey(
  String referencedTable,
  String referencedColumn, {
  String? onDelete,
  String? onUpdate,
}) {
  final constraint = StringBuffer();
  constraint.write("FOREIGN KEY REFERENCES $referencedTable($referencedColumn)");

  if (onDelete != null) {
    constraint.write(" ON DELETE $onDelete");
  }

  if (onUpdate != null) {
    constraint.write(" ON UPDATE $onUpdate");
  }

  _constraints.add(constraint.toString());
  return this;
}