From 6c16d52f3a3436c9ff8ae91a1f9dbd1ea6864445 Mon Sep 17 00:00:00 2001 From: Leo Batyuk Date: Tue, 18 May 2021 15:27:07 +0200 Subject: [PATCH 1/2] Add associations with composite primary keys * BelongsTo * HasMany * ManyToMany --- integration_test/support/migration.exs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/integration_test/support/migration.exs b/integration_test/support/migration.exs index fe4d62fc..27669db5 100644 --- a/integration_test/support/migration.exs +++ b/integration_test/support/migration.exs @@ -27,6 +27,8 @@ defmodule Ecto.Integration.Migration do add :intensity, :float add :author_id, :integer add :posted, :date + add :composite_a, :integer + add :composite_b, :integer timestamps(null: true) end @@ -113,6 +115,26 @@ defmodule Ecto.Integration.Migration do add :name, :string end + create table(:composite_pk_composite_pk, primary_key: false) do + add :b_1, :integer + add :a_1, references(:composite_pk, column: :a, with: [b_1: :b], type: :integer) + add :b_2, :integer + add :a_2, references(:composite_pk, column: :a, with: [b_2: :b], type: :integer) + end + + + alter table(:posts) do + modify :composite_a, references(:composite_pk, column: :a, with: [composite_b: :b], type: :integer) + end + + create table(:posts_composite_pk) do + add :post_id, references(:posts), primary_key: true + add :composite_a, references(:composite_pk, column: :a, with: [composite_b: :b], type: :integer), primary_key: true + add :composite_b, :integer, primary_key: true + end + + create unique_index(:posts_composite_pk, [:post_id, :composite_a, :composite_b]) + create table(:corrupted_pk, primary_key: false) do add :a, :string end From 006d5f001716de1443217afeccdf665d4484965e Mon Sep 17 00:00:00 2001 From: Leo B Date: Wed, 17 Nov 2021 18:51:40 +0100 Subject: [PATCH 2/2] Change for readability; allow testing has_one w/composite PKs --- integration_test/support/migration.exs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/integration_test/support/migration.exs b/integration_test/support/migration.exs index 27669db5..0fa81b69 100644 --- a/integration_test/support/migration.exs +++ b/integration_test/support/migration.exs @@ -116,10 +116,10 @@ defmodule Ecto.Integration.Migration do end create table(:composite_pk_composite_pk, primary_key: false) do - add :b_1, :integer add :a_1, references(:composite_pk, column: :a, with: [b_1: :b], type: :integer) - add :b_2, :integer + add :b_1, :integer add :a_2, references(:composite_pk, column: :a, with: [b_2: :b], type: :integer) + add :b_2, :integer end @@ -127,13 +127,13 @@ defmodule Ecto.Integration.Migration do modify :composite_a, references(:composite_pk, column: :a, with: [composite_b: :b], type: :integer) end - create table(:posts_composite_pk) do - add :post_id, references(:posts), primary_key: true - add :composite_a, references(:composite_pk, column: :a, with: [composite_b: :b], type: :integer), primary_key: true - add :composite_b, :integer, primary_key: true + create table(:one_to_one_composite_pk) do + add :composite_a, references(:composite_pk, column: :a, with: [composite_b: :b], type: :integer) + add :composite_b, :integer + timestamps() end - create unique_index(:posts_composite_pk, [:post_id, :composite_a, :composite_b]) + create unique_index(:one_to_one_composite_pk, [:composite_a, :composite_b]) create table(:corrupted_pk, primary_key: false) do add :a, :string