polymorphic_on_delete option needs refining and potentially moving
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Start by reproducing the example with the references configuration and polymorphic_on_delete :delete on each side of the relationship. Inspect the generated migration and compare the foreign-key delete behavior. Done requires an agreed configuration location or relationship-level design that correctly handles multiple polymorphic relationships.
Written by the indexing model from the issue text.
Description
Describe the bug
The references config block containing a polymorphic_on_delete :delete setting is on the "unexpected" side of the relationship between a resource and a polymorphic resource. If it is set on the polymorphic resource side, the generated migration does not implement the delete in the foreign key constraint.
Discussion here
https://elixirforum.com/t/how-to-combine-polymorphic-with-references-on-delete/60756
To Reproduce
defmodule MyApp.Location do
use Ash.Resource, data_layer: AshPostgres.DataLayer
postgres do
repo MyApp.Repo
polymorphic? true
# HAS NO EFFECT
references do
polymorphic_on_delete :delete
end
end
attributes do
uuid_primary_key :id
attribute :resource_id, :uuid do
allow_nil? false
private? true
end
attribute :x, :float
attribute :y, :float
end
end
defmodule MyApp.Event do
use Ash.Resource, data_layer: AshPostgres.DataLayer
postgres do
repo MyApp.Repo
table "events"
# WORKS IF IT IS HERE
references do
polymorphic_on_delete :delete
end
end
attributes do
uuid_primary_key :id
attribute :when, :utc_datetime, allow_nil?: false, default: &DateTime.utc_now/0
attribute :what, :string
attribute :who, :string
end
relationships do
has_one :location, Location do
relationship_context %{data_layer: %{table: "event_locations"}}
destination_attribute :resource_id
end
end
Expected behavior
To be agreed. Currently the config location is surprising as for other relationships it is the "owned" resource that defines the on_delete behaviour rather than the owning resource. However, where you have multiple relationships to polymorphic resources (to be expected!) and want to control the delete behaviour for each separately it may make sense to do it this way round. So there's a philosophical question to answer first - should the delete behaviour belong to the polymorphic resource or to the relationship? If it is the relationship it should probably be defined at the specific relationship level.
In the example above, this may be something like:
defmodule MyApp.Event do
use Ash.Resource, data_layer: AshPostgres.DataLayer
postgres do
repo MyApp.Repo
table "events"
references do
# Either something here that points to the Location resource, but looks different to
# the existing config for relationships as it is inverted...
end
end
attributes do
uuid_primary_key :id
attribute :when, :utc_datetime, allow_nil?: false, default: &DateTime.utc_now/0
attribute :what, :string
attribute :who, :string
end
relationships do
has_one :location, Location do
# Or probably better here to collect together all the implementation details of the relationship in one place
relationship_context %{data_layer: %{table: "event_locations"}, on_delete: :delete}
destination_attribute :resource_id
end
end
**Runtime
- Elixir version: 1.15.4
- Erlang version: Erlang/OTP 26 [erts-14.0.2]
- OS: Mac
- Ash version: 2.17.17
- Ash Postgres: 1.3.61
- any related extension versions
- Dominant language
- Elixir
- Stars
- 191
- Forks
- 186
- Avg merge
- 16h 4m
- Merged PRs (30d)
- 17
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ash-project/ash_postgres
-
Temporal: an update or destroy as of an instant doesn't carry into later versionsPossibly taken @hoanle0126 claimed this 14 days ago. Open
Difficulty 5/5 Over a week Newbie friendliness 15/100
ash-project/ash_postgres#915 · 1 comment ·
Maintainers usually reply within 1 day
-
has_create_action?/1 excludes manual creates, making the #49 nullability fix inert on AshEvents resourcesMay be free again @Mckay-Hinds claimed this 19 days ago, and no pull request is open. Open
Difficulty 3/5 1-2 days Newbie friendliness 76/100
ash-project/ash_postgres#851 · 9 comments ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 4/5 3-5 days Newbie friendliness 35/100
ash-project/ash_postgres#650 · 6 comments ·
Maintainers usually reply within 1 day
-
bug
Difficulty 4/5 3-5 days Newbie friendliness 35/100
ash-project/ash_postgres#642 · 5 comments ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 5/5 Over a week Newbie friendliness 40/100
ash-project/ash_postgres#628 · 1 comment ·
Maintainers usually reply within 1 day
All issues in ash-project/ash_postgres
Similar issues
-
Flaky testOpendette technique
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
etalab/transport-site#5701 ·
Maintainers usually reply within 5 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 75/100
Maintainers usually reply within 1 day
-
@swmansion/popcorn: published .d.ts files lack extensions on relative imports, breaking moduleResolution node16/nodenextPossibly taken @Huyvux12 claimed this 1 day ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
software-mansion/popcorn#929 · 2 comments ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
intellij-elixir/intellij-elixir#4705 ·
Maintainers usually reply within 1 day
-
Difficulty 1/5 Under an hour Newbie friendliness 85/100
phoenixframework/phoenix#6908 · 1 comment ·
Maintainers usually reply within 1 day