Hi,
I have the following migration files:
20210609132719_create_key_values.exs
defmodule MyApp.Repo.Migrations.CreateKeyValues do
use Ecto.Migration
def change do
create table(:key_values) do
add :type, :string, size: 20, null: false
add :key, :string, size: 8, null: false
add :value, :string, size: 40, null: false
timestamps()
end
create unique_index(:key_values, [:type, :key])
end
end
20210609132720_create_addresses.exs
defmodule MyApp.Repo.Migrations.CreateAddresses do
use Ecto.Migration
def change do
create table(:addresses) do
add(
:country_key,
references("key_values",
column: :key,
type: :string,
on_delete: :restrict,
on_update: :update_all,
with: [type: "country"]
)
)
timestamps()
end
end
end
When I run the migration, I get the following error:
09:03:23.118 [info] == Running 20210609132719 MyApp.Repo.Migrations.CreateKeyValues.change/0 forward
09:03:23.121 [info] create table key_values
09:03:23.128 [info] create index key_values_type_key_index
09:03:23.133 [info] == Migrated 20210609132719 in 0.0s
09:03:23.158 [info] == Running 20210609132720 MyApp.Repo.Migrations.CreateAddresses.change/0 forward
09:03:23.158 [info] create table addresses
** (Postgrex.Error) ERROR 42703 (undefined_column) column "type" referenced in foreign key constraint does not exist
(ecto_sql 3.6.2) lib/ecto/adapters/sql.ex:760: Ecto.Adapters.SQL.raise_sql_call_error/1
(elixir 1.12.0) lib/enum.ex:1553: Enum."-map/2-lists^map/1-0-"/2
(ecto_sql 3.6.2) lib/ecto/adapters/sql.ex:848: Ecto.Adapters.SQL.execute_ddl/4
(ecto_sql 3.6.2) lib/ecto/migration/runner.ex:343: Ecto.Migration.Runner.log_and_execute_ddl/3
(ecto_sql 3.6.2) lib/ecto/migration/runner.ex:117: anonymous fn/6 in Ecto.Migration.Runner.flush/0
(elixir 1.12.0) lib/enum.ex:2356: Enum."-reduce/3-lists^foldl/2-0-"/3
(ecto_sql 3.6.2) lib/ecto/migration/runner.ex:116: Ecto.Migration.Runner.flush/0
(ecto_sql 3.6.2) lib/ecto/migration/runner.ex:280: Ecto.Migration.Runner.perform_operation/3
I searched, but could not find any relevant links on the web, to explain this error. Can someone help me with my understanding of where I am going wrong?