diff --git a/test/cascade_destroy_test.exs b/test/cascade_destroy_test.exs new file mode 100644 index 00000000..a407ea35 --- /dev/null +++ b/test/cascade_destroy_test.exs @@ -0,0 +1,25 @@ +defmodule AshPostgresTest.CascadeDestroyTest do + use AshPostgres.RepoCase, async: true + + alias AshPostgres.Test.{Post, Rating} + + test "can cascade destroy a has_many with parent filter" do + post = + Post.create!("post", %{score: 1}) + + Rating + |> Ash.Changeset.for_create(:create, %{score: 2, resource_id: post.id}) + |> Ash.Changeset.set_context(%{data_layer: %{table: "post_ratings"}}) + |> Ash.create!() + + post + |> Ash.Changeset.for_destroy(:cascade_destroy) + |> Ash.destroy!() + + assert [] = + Rating + |> Ash.Query.for_read(:read) + |> Ash.Query.set_context(%{data_layer: %{table: "post_ratings"}}) + |> Ash.read!() + end +end diff --git a/test/support/resources/post.ex b/test/support/resources/post.ex index d41f4198..d221bdd6 100644 --- a/test/support/resources/post.ex +++ b/test/support/resources/post.ex @@ -230,6 +230,10 @@ defmodule AshPostgres.Test.Post do ) end + destroy :cascade_destroy do + change(cascade_destroy(:high_ratings, after_action?: false)) + end + update :update do primary?(true) require_atomic?(false) @@ -521,6 +525,13 @@ defmodule AshPostgres.Test.Post do relationship_context: %{data_layer: %{table: "post_ratings"}} ) + has_many :high_ratings, AshPostgres.Test.Rating do + public?(true) + destination_attribute(:resource_id) + relationship_context(%{data_layer: %{table: "post_ratings"}}) + filter(expr(score > parent(score))) + end + has_many(:post_links, AshPostgres.Test.PostLink, public?: true, destination_attribute: :source_post_id,