File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ defmodule AshPostgresTest.CascadeDestroyTest do
2
+ use AshPostgres.RepoCase , async: true
3
+
4
+ alias AshPostgres.Test . { Post , Rating }
5
+
6
+ test "can cascade destroy a has_many with parent filter" do
7
+ post =
8
+ Post . create! ( "post" , % { score: 1 } )
9
+
10
+ Rating
11
+ |> Ash.Changeset . for_create ( :create , % { score: 2 , resource_id: post . id } )
12
+ |> Ash.Changeset . set_context ( % { data_layer: % { table: "post_ratings" } } )
13
+ |> Ash . create! ( )
14
+
15
+ post
16
+ |> Ash.Changeset . for_destroy ( :cascade_destroy )
17
+ |> Ash . destroy! ( )
18
+
19
+ assert [ ] =
20
+ Rating
21
+ |> Ash.Query . for_read ( :read )
22
+ |> Ash.Query . set_context ( % { data_layer: % { table: "post_ratings" } } )
23
+ |> Ash . read! ( )
24
+ end
25
+ end
Original file line number Diff line number Diff line change @@ -230,6 +230,10 @@ defmodule AshPostgres.Test.Post do
230
230
)
231
231
end
232
232
233
+ destroy :cascade_destroy do
234
+ change ( cascade_destroy ( :high_ratings , after_action?: false ) )
235
+ end
236
+
233
237
update :update do
234
238
primary? ( true )
235
239
require_atomic? ( false )
@@ -521,6 +525,13 @@ defmodule AshPostgres.Test.Post do
521
525
relationship_context: % { data_layer: % { table: "post_ratings" } }
522
526
)
523
527
528
+ has_many :high_ratings , AshPostgres.Test.Rating do
529
+ public? ( true )
530
+ destination_attribute ( :resource_id )
531
+ relationship_context ( % { data_layer: % { table: "post_ratings" } } )
532
+ filter ( expr ( score > parent ( score ) ) )
533
+ end
534
+
524
535
has_many ( :post_links , AshPostgres.Test.PostLink ,
525
536
public?: true ,
526
537
destination_attribute: :source_post_id ,
You can’t perform that action at this time.
0 commit comments