-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Support associations with composite foreign keys #3638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4a0264d
fefee0c
f29c121
ce380a8
1d68ad0
e2dd4a5
1ed34b4
ce651f3
b21a4e7
9e18f75
ca19a2a
02146bd
2b5f584
fb2afcc
dc652ac
b8665ce
0333ffc
658060d
d02709c
66d5299
13c6f8e
527354d
edb05bd
f3ae0cd
e82f12d
65a6331
a6588ac
458e704
d887cb3
304df11
f73f511
8551091
1573854
d42688c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,8 @@ defmodule Ecto.Integration.Post do | |
has_one :update_permalink, Ecto.Integration.Permalink, foreign_key: :post_id, on_delete: :delete_all, on_replace: :update | ||
has_many :comments_authors, through: [:comments, :author] | ||
belongs_to :author, Ecto.Integration.User | ||
belongs_to :composite, Ecto.Integration.CompositePk, | ||
foreign_key: [:composite_a, :composite_b], references: [:a, :b], type: [:integer, :integer], on_replace: :nilify | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the key names here can probably be inferred from the models themselves; I'm not sure if this is something I should target in this PR, too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think being explicit is probably better for now. |
||
many_to_many :users, Ecto.Integration.User, | ||
join_through: "posts_users", on_delete: :delete_all, on_replace: :delete | ||
many_to_many :ordered_users, Ecto.Integration.User, join_through: "posts_users", preload_order: [desc: :name] | ||
|
@@ -63,7 +65,7 @@ defmodule Ecto.Integration.Post do | |
join_through: Ecto.Integration.PostUserCompositePk | ||
has_many :users_comments, through: [:users, :comments] | ||
has_many :comments_authors_permalinks, through: [:comments_authors, :permalink] | ||
has_one :post_user_composite_pk, Ecto.Integration.PostUserCompositePk | ||
has_many :post_user_composite_pk, Ecto.Integration.PostUserCompositePk | ||
timestamps() | ||
end | ||
|
||
|
@@ -294,6 +296,12 @@ defmodule Ecto.Integration.CompositePk do | |
field :a, :integer, primary_key: true | ||
field :b, :integer, primary_key: true | ||
field :name, :string | ||
has_many :posts, Ecto.Integration.Post, foreign_key: [:composite_a, :composite_b], references: [:a, :b] | ||
many_to_many :composites, Ecto.Integration.CompositePk, | ||
join_through: "composite_pk_composite_pk", join_keys: [[a_1: :a, b_1: :b], [a_2: :a, b_2: :b]], | ||
on_delete: :delete_all, on_replace: :delete | ||
has_one :one_to_one_composite_pk, Ecto.Integration.OneToOneCompositePk, | ||
foreign_key: [:composite_a, :composite_b], references: [:a, :b] | ||
end | ||
def changeset(schema, params) do | ||
cast(schema, params, ~w(a b name)a) | ||
|
@@ -332,6 +340,23 @@ defmodule Ecto.Integration.PostUserCompositePk do | |
end | ||
end | ||
|
||
defmodule Ecto.Integration.OneToOneCompositePk do | ||
@moduledoc """ | ||
This module is used to test: | ||
|
||
* Composite primary keys for 2 has_one fields | ||
|
||
""" | ||
use Ecto.Integration.Schema | ||
|
||
schema "one_to_one_composite_pk" do | ||
belongs_to :composite, Ecto.Integration.CompositePk, | ||
foreign_key: [:composite_a, :composite_b], references: [:a, :b], type: [:integer, :integer], on_replace: :nilify | ||
timestamps() | ||
end | ||
end | ||
|
||
|
||
defmodule Ecto.Integration.Usec do | ||
@moduledoc """ | ||
This module is used to test: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This branch needs a few changes to the base migration used for the integration tests; I've opened a PR for this so I hope to be able to drop this line soon.