File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -2682,8 +2682,15 @@ defmodule AshPostgres.DataLayer do
2682
2682
resource
2683
2683
|> Ash.Resource.Info . relationships ( )
2684
2684
|> Enum . reduce ( changeset , fn relationship , changeset ->
2685
+ # Check if there's a custom reference name defined in the DSL
2685
2686
name =
2686
- "#{ AshPostgres.DataLayer.Info . table ( resource ) } _#{ relationship . source_attribute } _fkey"
2687
+ case AshPostgres.DataLayer.Info . reference ( resource , relationship . name ) do
2688
+ % { name: custom_name } when not is_nil ( custom_name ) ->
2689
+ custom_name
2690
+
2691
+ _ ->
2692
+ "#{ AshPostgres.DataLayer.Info . table ( resource ) } _#{ relationship . source_attribute } _fkey"
2693
+ end
2687
2694
2688
2695
case repo . default_constraint_match_type ( :foreign , name ) do
2689
2696
{ :regex , regex } ->
Original file line number Diff line number Diff line change @@ -105,4 +105,27 @@ defmodule AshPostgres.ReferencesTest do
105
105
end
106
106
end
107
107
end
108
+
109
+ test "named reference results in properly applied foreign_key_constraint/3 on the underlying changeset" do
110
+ # Create a comment with an invalid post_id
111
+ assert { :error , % Ash.Error.Invalid { errors: errors } } =
112
+ AshPostgres.Test.Comment
113
+ |> Ash.Changeset . for_create ( :create , % {
114
+ title: "Test Comment" ,
115
+ # This post doesn't exist
116
+ post_id: Ash.UUID . generate ( )
117
+ } )
118
+ |> Ash . create ( )
119
+
120
+ assert [
121
+ % Ash.Error.Changes.InvalidAttribute {
122
+ field: :post_id ,
123
+ message: "does not exist" ,
124
+ private_vars: private_vars
125
+ }
126
+ ] = errors
127
+
128
+ assert Keyword . get ( private_vars , :constraint ) == "special_name_fkey"
129
+ assert Keyword . get ( private_vars , :constraint_type ) == :foreign_key
130
+ end
108
131
end
You can’t perform that action at this time.
0 commit comments