EctoUnnest.Constraints (ecto_unnest v0.3.2)

Copy Markdown View Source

Foreign-key introspection for EctoUnnest.sync_all/4.

A sync deletes the rows the source no longer has, and a delete is the one part of the operation that can destroy data the source never described: a child row reached through ON DELETE CASCADE/SET NULL/SET DEFAULT disappears (or is blanked) with no error. NO ACTION/RESTRICT children are safe by construction — Postgres raises foreign_key_violation and the surrounding transaction rolls the whole sync back — so the modes that need catalog knowledge are the ones that must either refuse up front (:restrict) or step around the referenced rows (:skip_referenced).

referencing/3 reads pg_constraint for the foreign keys pointing at the table, including the column pairing for composite keys (conkey/confkey are attribute-number arrays, ordered).

Summary

Functions

Human-readable table.columns -> ON DELETE ACTION (constraint) for error messages.

The subset of fks whose ON DELETE action silently changes rows in the referencing table (CASCADE, SET NULL, SET DEFAULT).

The quoted, optionally schema-qualified identifier of table, for a ::regclass cast.

The foreign keys that reference table (optionally in prefix).

Types

fk()

@type fk() :: %{
  name: String.t(),
  prefix: String.t(),
  table: String.t(),
  columns: [atom()],
  references: [atom()],
  on_delete: :no_action | :restrict | :cascade | :set_null | :set_default
}

Functions

describe(fk)

@spec describe(fk()) :: String.t()

Human-readable table.columns -> ON DELETE ACTION (constraint) for error messages.

destructive(fks)

@spec destructive([fk()]) :: [fk()]

The subset of fks whose ON DELETE action silently changes rows in the referencing table (CASCADE, SET NULL, SET DEFAULT).

qualify(table, prefix)

@spec qualify(String.t(), String.t() | nil) :: String.t()

The quoted, optionally schema-qualified identifier of table, for a ::regclass cast.

The table name comes from a schema's __schema__(:source) (or an explicit binary source), never from user input; quoting keeps mixed-case identifiers resolvable and takes ::regclass out of search_path guesswork.

referencing(repo, table, prefix \\ nil)

@spec referencing(Ecto.Repo.t(), String.t(), String.t() | nil) :: [fk()]

The foreign keys that reference table (optionally in prefix).

Each entry names the referencing table, its columns, the parent columns they point at (paired positionally, so composite keys survive), and the declared ON DELETE action.