improvement: add relationships to underlying ecto schemas for better escape hatches

This commit is contained in:
Zach Daniel 2023-01-27 11:20:08 -05:00
parent bfc592adab
commit 2787b5074b

View file

@ -156,6 +156,43 @@ defmodule Ash.Schema do
for relationship <- Ash.Resource.Info.relationships(__MODULE__),
relationship.name not in Ash.Resource.reserved_names() do
case relationship do
%{no_attributes?: true} ->
:ok
%{manual?: true} ->
:ok
%{manual: manual} when not is_nil(manual) ->
:ok
%{type: :belongs_to} ->
belongs_to relationship.name, relationship.destination,
define_field: false,
references: relationship.destination_attribute,
foreign_key: relationship.source_attribute
%{type: :has_many} ->
has_many relationship.name, relationship.destination,
foreign_key: relationship.destination_attribute,
references: relationship.source_attribute
%{type: :has_one} ->
has_one relationship.name, relationship.destination,
foreign_key: relationship.destination_attribute,
references: relationship.source_attribute
%{type: :many_to_many} ->
many_to_many relationship.name, relationship.destination,
join_through: relationship.through,
join_keys: [
{relationship.source_attribute_on_join_resource,
relationship.source_attribute},
{relationship.destination_attribute_on_join_resource,
relationship.destination_attribute}
]
end
Module.put_attribute(
__MODULE__,
:ash_struct_fields,