Skip to content

Commit 2477a9b

Browse files
soundmonsterLeo B
authored and
Leo B
committed
Add associations with composite primary keys
* BelongsTo * HasMany * ManyToMany
1 parent c8b9c57 commit 2477a9b

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

integration_test/support/migration.exs

+23
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ defmodule Ecto.Integration.Migration do
2727
add :intensity, :float
2828
add :author_id, :integer
2929
add :posted, :date
30+
add :composite_a, :integer
31+
add :composite_b, :integer
3032
timestamps(null: true)
3133
end
3234

@@ -108,6 +110,27 @@ defmodule Ecto.Integration.Migration do
108110
add :name, :string
109111
end
110112

113+
create table(:composite_pk_composite_pk, primary_key: false) do
114+
add :b_1, :integer
115+
add :a_1, references(:composite_pk, column: :a, with: [b_1: :b], type: :integer)
116+
add :b_2, :integer
117+
add :a_2, references(:composite_pk, column: :a, with: [b_2: :b], type: :integer)
118+
end
119+
120+
121+
# TODO not sure if it is important to have a foreign keys defined properly; see other associations in "posts"
122+
alter table(:posts) do
123+
modify :composite_a, references(:composite_pk, column: :a, with: [composite_b: :b], type: :integer)
124+
end
125+
126+
create table(:posts_composite_pk) do
127+
add :post_id, references(:posts), primary_key: true
128+
add :composite_a, references(:composite_pk, column: :a, with: [composite_b: :b], type: :integer), primary_key: true
129+
add :composite_b, :integer, primary_key: true
130+
end
131+
132+
create unique_index(:posts_composite_pk, [:post_id, :composite_a, :composite_b])
133+
111134
create table(:corrupted_pk, primary_key: false) do
112135
add :a, :string
113136
end

0 commit comments

Comments
 (0)