Bylaw.Db.Adapters.Postgres.Checks.ForeignKeyNullability
(bylaw_postgres v0.2.0)
Copy Markdown
View Source
Validates that Postgres foreign key columns are not nullable.
Examples
Before, the foreign key allows missing parents:
CREATE TABLE orders (
id uuid PRIMARY KEY,
account_id uuid REFERENCES accounts(id)
);That makes the association optional even if the application treats every order
as belonging to an account. Code then has to handle impossible NULL cases.
After, make the required relationship non-nullable:
CREATE TABLE orders (
id uuid PRIMARY KEY,
account_id uuid NOT NULL REFERENCES accounts(id)
);The database shape now matches the domain model, and callers can rely on the relationship being present.
Notes
This check only inspects columns that are already part of a foreign key
constraint. Optional relationships should be excluded with an except
matcher.
Options
:validate- explicitfalsedisables this check.:rules- optional rule keyword list or non-empty list of rule keyword lists. Rules use only shared scope keys.
Run globally with defaults:
Bylaw.Db.Adapters.Postgres.Checks.ForeignKeyNullabilityRun only for matching rule scopes:
{Bylaw.Db.Adapters.Postgres.Checks.ForeignKeyNullability,
rules: [where: [schemas: ["public"]]]}
{Bylaw.Db.Adapters.Postgres.Checks.ForeignKeyNullability,
rules: [
where: [schemas: ["public"]],
except: [
[tables: ["runs"], columns: ["assistant_message_id"]],
[constraints: ["messages_parent_message_id_fkey"]]
]
]}Usage
Add this module to the checks passed to
Bylaw.Db.Adapters.Postgres.validate/2. See the
README usage section for the full ExUnit setup.
Summary
Functions
Implements the Bylaw.Db.Check validation callback.
Types
@type check_opts() :: [check_opt()]
@type matcher() :: [ schema: matcher_values(), table: matcher_values(), constraint: matcher_values(), column: matcher_values() ]
@type matcher_values() :: [matcher_value()]
Functions
@spec validate(target :: Bylaw.Db.Target.t(), opts :: check_opts()) :: Bylaw.Db.Check.result()
Implements the Bylaw.Db.Check validation callback.