The relationship graph over registered resources, with PostgREST's embedding resolution rules.
Relationships come from these places, none of which touch the database:
Ecto associations:
belongs_togives:m2o(and the inverse:o2m),has_many/has_onethe same pair. The foreign key constraint name defaults to"<table>_<column>_fkey".Junctions, as in PostgREST: a registered resource with
:m2orelationships to two tables whose key columns all lie in its primary key yields:m2min both directions (self joins included).many_to_manyassociations are not a source; their name is accepted as an embed alias for the derived relationship.Overrides in the resource's
relationships:option keyed by association name:[constraint: "client"]renames the constraint,[cardinality: :one_to_one]makes the pair:o2o.Explicit declarations in
relationships:, needed for composite keys:relationships: [ students_info: [ kind: :o2o, target: MyApp.StudentsInfo, columns: [{"code", "code"}, {"id", "id"}], constraint: "students_info_code_id_fkey" ], files: [ kind: :m2m, target: MyApp.Files, junction: [ table: "touched_files", schema: "public", source_columns: [{"user_id", "user_id"}], target_columns: [{"filename", "filename"}], constraints: {"fk_users_tasks", "fk_upload"} ] ] ]An explicit
:m2myields both directions and its junction table must be a registered resource (ArgumentErrorotherwise, as for an unregisteredtarget:).:m2odeclares the foreign key on this resource;:o2mand:o2odeclare it on the target.columnspairs are{source_column, target_column}.
Every foreign key yields both directions, so a relationship is discoverable from either resource even when only one side declares it.
Summary
Functions
Builds the graph for the given resource modules. Ecto associations to other modules
are ignored; explicit declarations naming an unregistered target or junction raise
ArgumentError.
PGRST200 for an embed name from parent, a source that is no registered resource and so
has no relationships (a function's composite result): as PostgREST, the hint names the
closest table of schema that has relationships.
Resolves the embed name (with optional hint) from source.
The relationships a request against schema can embed: those whose source and target
tables both live in schema. PostgREST keys its relationships by the foreign table's
schema and looks them up with the request schema, so a foreign key into another exposed
schema is not embeddable (PGRST200), even though it is in the full graph.
Types
@type t() :: %PgRest.SchemaGraph{ rels: %{required(module()) => [PgRest.SchemaGraph.Relationship.t()]}, tables: %{required(module()) => {schema :: String.t(), table :: String.t()}} }
Functions
Builds the graph for the given resource modules. Ecto associations to other modules
are ignored; explicit declarations naming an unregistered target or junction raise
ArgumentError.
@spec no_relationship_from(t(), String.t(), String.t(), String.t(), String.t() | nil) :: PgRest.Error.t()
PGRST200 for an embed name from parent, a source that is no registered resource and so
has no relationships (a function's composite result): as PostgREST, the hint names the
closest table of schema that has relationships.
@spec resolve(t(), module(), String.t(), String.t() | nil) :: {:ok, PgRest.SchemaGraph.Relationship.t()} | {:error, PgRest.Error.t()}
Resolves the embed name (with optional hint) from source.
Mirrors PostgREST's findRel: the name is a target table, a constraint or a
single foreign-key column of the source; the hint a constraint, a single key
column on either side, or a junction table. A name that is an association name
on source is accepted when nothing else matches.
Errors are PGRST200 (nothing matches, 400) and PGRST201 (several match, 300).
The relationships a request against schema can embed: those whose source and target
tables both live in schema. PostgREST keys its relationships by the foreign table's
schema and looks them up with the request schema, so a foreign key into another exposed
schema is not embeddable (PGRST200), even though it is in the full graph.