Hello, I am trying to create a relation has_may with a custom primary_key :
My schemes are the following
defmodule Example.Accounts.User do
use Ecto.Schema
import Ecto.Changeset
alias Example.Accounts.User
@primary_key{:identification, :id, []}
@derive {Phoenix.Param, key: :identification}
schema "users" do
field :name, :string
has_many :properties, Example.Properties.Property
timestamps()
end
@doc false
def changeset(%User{} = user, attrs) do
user
|> cast(attrs, [:identification, :name])
|> validate_required([:identification, :name])
end
end
And
defmodule Example.Properties.Property do
use Ecto.Schema
import Ecto.Changeset
alias Example.Properties.Property
schema "properties" do
field :n_property, :integer
field :proprietor, :id
belongs_to :user, Example.Accounts.User
timestamps()
end
@doc false
def changeset(%Property{} = property, attrs) do
property
|> cast(attrs, [:n_property])
|> validate_required([:n_property])
end
end
My migrations files are the following:
defmodule Example.Repo.Migrations.CreateUsers do
use Ecto.Migration
def change do
create table(:users, primary_key: false) do
add :identification, :id, primary_key: true
add :name, :string
timestamps()
end
end
end
And
defmodule Example.Repo.Migrations.CreateProperties do
use Ecto.Migration
def change do
create table(:properties) do
add :n_property, :integer
add :proprietor, references("users")
timestamps()
end
end
end
But when I run mix ecto.migrate, I get the following error
(Postgrex.Error) ERROR 42703 (undefined_column): There is no column "id" referenced in the foreign key
The id column is the default primary key for the table user but in this case should be “identification” according to the configuration of the schema…