Drops.SQL.Database.ForeignKey (drops_relation v0.1.0)
View SourceRepresents a foreign key constraint in a database table.
This struct stores information about foreign key relationships, supporting both single-column and composite foreign keys. The columns attribute contains the names of columns in the current table that reference another table.
Examples
# Simple foreign key
%Drops.SQL.Database.ForeignKey{
name: "fk_posts_user_id",
columns: ["user_id"],
referenced_table: "users",
referenced_columns: ["id"],
on_delete: :delete_all,
on_update: :restrict
}
# Composite foreign key
%Drops.SQL.Database.ForeignKey{
name: "fk_user_roles_composite",
columns: ["user_id", "role_id"],
referenced_table: "user_role_assignments",
referenced_columns: ["user_id", "role_id"],
on_delete: :cascade,
on_update: :cascade
}
Summary
Functions
Creates a new ForeignKey struct.
Types
@type action() ::
:restrict
| :cascade
| :set_null
| :set_default
| :delete_all
| :nilify_all
| nil
Functions
Creates a new ForeignKey struct.
Parameters
name
- The constraint name (optional)columns
- List of column names in the current tablereferenced_table
- The name of the referenced tablereferenced_columns
- List of column names in the referenced tableon_delete
- Action to take when referenced row is deletedon_update
- Action to take when referenced row is updated
Examples
iex> Drops.SQL.Database.ForeignKey.new(
...> "fk_posts_user_id",
...> ["user_id"],
...> "users",
...> ["id"],
...> :delete_all,
...> :restrict
...> )
%Drops.SQL.Database.ForeignKey{
name: "fk_posts_user_id",
columns: ["user_id"],
referenced_table: "users",
referenced_columns: ["id"],
on_delete: :delete_all,
on_update: :restrict
}