Trying to copy trello example, can't make changeset work with foreignkeys:
Migration:
defmodule Final.Repo.Migrations.CreateKw do
use Ecto.Migration
def change do
create table(:kw) do
add :keyo_id, references(:keyo), null: false
add :users_id, references(:users), null: false
timestamps
end
create index(:kw, [:keyo_id])
create index(:kw, [:users_id])
create unique_index(:kw, [:keyo_id, :users_id])
end
end
Model:
defmodule Final.Kw do
use Final.Web, :model
alias Final.Repo
schema "kw" do
belongs_to :keyo, Final.Keyo
belongs_to :user, Final.User
timestamps
end
@required_fields ~w(keyo_id users_id)
@optional_fields ~w()
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
Full error:
** (ArgumentError) unknown field
users_id
(note only fields, embeds, belongs_to, has_one and has_many associations are supported in changesets)
Command causing it:
changeset = Final.Kw.changeset(%Final.Kw{}, %{keyo_id: 1, users_id: 2})
My code is pretty much the same as on the example, I tried every possible combination but can't make it work, what am I missing ?
validate_required
. – Robin Clowers