Drops.SQL.Database.ForeignKey (drops_relation v0.1.0)

View Source

Represents 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

Types

action()

@type action() ::
  :restrict
  | :cascade
  | :set_null
  | :set_default
  | :delete_all
  | :nilify_all
  | nil

meta()

@type meta() :: %{on_delete: action(), on_update: action()}

t()

@type t() :: %Drops.SQL.Database.ForeignKey{
  columns: [String.t()],
  meta: term(),
  name: String.t() | nil,
  referenced_columns: [String.t()],
  referenced_table: String.t()
}

Functions

new(name, columns, referenced_table, referenced_columns, meta)

@spec new(String.t(), [String.t()], String.t(), [String.t()], meta()) :: t()

Creates a new ForeignKey struct.

Parameters

  • name - The constraint name (optional)
  • columns - List of column names in the current table
  • referenced_table - The name of the referenced table
  • referenced_columns - List of column names in the referenced table
  • on_delete - Action to take when referenced row is deleted
  • on_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
}