diff --git a/lib/ash/resource/relationships/many_to_many.ex b/lib/ash/resource/relationships/many_to_many.ex index c14c5d11..70fc587f 100644 --- a/lib/ash/resource/relationships/many_to_many.ex +++ b/lib/ash/resource/relationships/many_to_many.ex @@ -37,6 +37,7 @@ defmodule Ash.Resource.Relationships.ManyToMany do ], destination_field_on_join_table: [ type: :atom, + required: true, doc: "The field on the join table that should line up with `destination_field` on the related resource. Default: [relationshihp_name]_id" ], @@ -90,8 +91,7 @@ defmodule Ash.Resource.Relationships.ManyToMany do source_field: opts[:source_field], destination_field: opts[:destination_field], source_field_on_join_table: opts[:source_field_on_join_table], - destination_field_on_join_table: - opts[:destination_field_on_join_table] || :"#{name}_id" + destination_field_on_join_table: opts[:destination_field_on_join_table] }} {:error, errors} -> diff --git a/test/actions/create_test.exs b/test/actions/create_test.exs index 56c5227c..7e5bbac5 100644 --- a/test/actions/create_test.exs +++ b/test/actions/create_test.exs @@ -40,9 +40,11 @@ defmodule Ash.Test.Actions.CreateTest do end relationships do - has_one :profile, Profile + has_one :profile, Profile, destination_field: :author_id - has_many :posts, Ash.Test.Actions.CreateTest.Post, reverse_relationship: :author + has_many :posts, Ash.Test.Actions.CreateTest.Post, + reverse_relationship: :author, + destination_field: :author end end diff --git a/test/actions/destroy_test.exs b/test/actions/destroy_test.exs index 42274cbe..78a67d08 100644 --- a/test/actions/destroy_test.exs +++ b/test/actions/destroy_test.exs @@ -42,9 +42,9 @@ defmodule Ash.Test.Actions.DestroyTest do end relationships do - has_one :profile, Profile + has_one :profile, Profile, destination_field: :author_id - has_many :posts, Ash.Test.Actions.CreateTest.Post + has_many :posts, Ash.Test.Actions.CreateTest.Post, destination_field: :author_id end end diff --git a/test/actions/read_test.exs b/test/actions/read_test.exs index 95e6861f..e9864662 100644 --- a/test/actions/read_test.exs +++ b/test/actions/read_test.exs @@ -18,7 +18,7 @@ defmodule Ash.Test.Actions.ReadTest do end relationships do - has_many :posts, Ash.Test.Actions.ReadTest.Post + has_many :posts, Ash.Test.Actions.ReadTest.Post, destination_field: :author end end diff --git a/test/actions/side_load_test.exs b/test/actions/side_load_test.exs index 2d13d2f8..119c79d4 100644 --- a/test/actions/side_load_test.exs +++ b/test/actions/side_load_test.exs @@ -18,7 +18,9 @@ defmodule Ash.Test.Actions.SideLoadTest do end relationships do - has_many :posts, Ash.Test.Actions.SideLoadTest.Post, reverse_relationship: :author + has_many :posts, Ash.Test.Actions.SideLoadTest.Post, + reverse_relationship: :author, + destination_field: :author_id end end diff --git a/test/actions/update_test.exs b/test/actions/update_test.exs index 222ce9ba..fd88d424 100644 --- a/test/actions/update_test.exs +++ b/test/actions/update_test.exs @@ -40,9 +40,11 @@ defmodule Ash.Test.Actions.UpdateTest do end relationships do - has_one :profile, Profile + has_one :profile, Profile, destination_field: :author_id - has_many :posts, Ash.Test.Actions.UpdateTest.Post, reverse_relationship: :author + has_many :posts, Ash.Test.Actions.UpdateTest.Post, + reverse_relationship: :author, + destination_field: :author_id end end diff --git a/test/filter/filter_test.exs b/test/filter/filter_test.exs index 7f3d6a83..a68f0639 100644 --- a/test/filter/filter_test.exs +++ b/test/filter/filter_test.exs @@ -49,7 +49,7 @@ defmodule Ash.Test.Filter.FilterTest do reverse_relationship: :author2, destination_field: :author1_id - has_one :profile, Profile + has_one :profile, Profile, destination_field: :user_id end end diff --git a/test/resource/relationships/has_many_test.exs b/test/resource/relationships/has_many_test.exs index 9ab60ac8..4e1c6cb8 100644 --- a/test/resource/relationships/has_many_test.exs +++ b/test/resource/relationships/has_many_test.exs @@ -21,7 +21,7 @@ defmodule Ash.Test.Resource.Relationshihps.HasManyTest do test "it creates a relationship" do defposts do relationships do - has_many :foobar, FooBar + has_many :foobar, FooBar, destination_field: :post_id end end @@ -60,7 +60,7 @@ defmodule Ash.Test.Resource.Relationshihps.HasManyTest do fn -> defposts do relationships do - has_many :foobar, FooBar, source_field: "foo" + has_many :foobar, FooBar, source_field: "foo", destination_field: :post_id end end end diff --git a/test/resource/relationships/has_one_test.exs b/test/resource/relationships/has_one_test.exs index 3684b3be..2e0d692b 100644 --- a/test/resource/relationships/has_one_test.exs +++ b/test/resource/relationships/has_one_test.exs @@ -21,7 +21,7 @@ defmodule Ash.Test.Resource.Relationshihps.HasOneTest do test "it creates a relationship" do defposts do relationships do - has_one :foobar, FooBar + has_one :foobar, FooBar, destination_field: :post_id end end @@ -60,7 +60,7 @@ defmodule Ash.Test.Resource.Relationshihps.HasOneTest do fn -> defposts do relationships do - has_one :foobar, FooBar, source_field: "foo" + has_one :foobar, FooBar, source_field: "foo", destination_field: :post_id end end end diff --git a/test/resource/relationships/many_to_many_test.exs b/test/resource/relationships/many_to_many_test.exs index 78fbeb91..7c026a04 100644 --- a/test/resource/relationships/many_to_many_test.exs +++ b/test/resource/relationships/many_to_many_test.exs @@ -21,7 +21,10 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do test "it creates a relationship and a join relationship" do defposts do relationships do - many_to_many :related_posts, Post, through: SomeResource + many_to_many :related_posts, Post, + through: SomeResource, + source_field_on_join_table: :post_id, + destination_field_on_join_table: :related_post_id end end @@ -29,7 +32,7 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do %Ash.Resource.Relationships.HasMany{ cardinality: :many, destination: SomeResource, - destination_field: :posts_id, + destination_field: :post_id, name: :related_posts_join_assoc, source: Ash.Test.Resource.Relationships.ManyToManyTest.Post, source_field: :id, @@ -40,11 +43,11 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do cardinality: :many, destination: Ash.Test.Resource.Relationships.ManyToManyTest.Post, destination_field: :id, - destination_field_on_join_table: :related_posts_id, + destination_field_on_join_table: :related_post_id, name: :related_posts, source: Ash.Test.Resource.Relationships.ManyToManyTest.Post, source_field: :id, - source_field_on_join_table: :posts_id, + source_field_on_join_table: :post_id, through: SomeResource, type: :many_to_many, reverse_relationship: nil @@ -61,7 +64,10 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do fn -> defposts do relationships do - many_to_many :foobars, Foobar, through: "some_table" + many_to_many :foobars, Foobar, + through: "some_table", + source_field_on_join_table: :source_post_id, + destination_field_on_join_table: :destination_post_id end end end @@ -71,7 +77,10 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do test "you can pass a module to `through`" do defposts do relationships do - many_to_many :foobars, Foobar, through: FooBars + many_to_many :foobars, Foobar, + through: FooBars, + source_field_on_join_table: :source_post_id, + destination_field_on_join_table: :destination_post_id end end end @@ -83,7 +92,10 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do fn -> defposts do relationships do - many_to_many :foobars, Foobar, through: FooBars, source_field_on_join_table: "what" + many_to_many :foobars, Foobar, + through: FooBars, + source_field_on_join_table: "what", + destination_field_on_join_table: :destination_post_id end end end @@ -99,7 +111,8 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do relationships do many_to_many :foobars, Foobar, through: FooBar, - destination_field_on_join_table: "what" + destination_field_on_join_table: "what", + source_field_on_join_table: :source_post_id end end end @@ -115,7 +128,9 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do relationships do many_to_many :foobars, Foobar, through: FooBar, - source_field: "what" + source_field: "what", + source_field_on_join_table: :source_post_id, + destination_field_on_join_table: :destination_post_id end end end @@ -131,7 +146,9 @@ defmodule Ash.Test.Resource.Relationships.ManyToManyTest do relationships do many_to_many :foobars, Foobar, through: FooBars, - destination_field: "what" + destination_field: "what", + source_field_on_join_table: :source_post_id, + destination_field_on_join_table: :destination_post_id end end end