I am inserting a model A that contains a foreign key to another model B.
defmodule MyApp.ModelA do
use MyApp.Web, :model
schema "model_a" do
field :type, :string, null: false
field :data, :string, null: false
belongs_to :model_b, MyApp.ModelB
timestamps()
end
@required_fields ~w(type data)
@optional_fields ~w()
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields, @optional_fields)
|> assoc_constraint(:model_b)
end
end
and the Model B:
defmodule MyApp.ModelB do
use MyApp.Web, :model
schema "model_b" do
field :username, :string
field :pass, :string
has_many :model_a, MyApp.ModelA
timestamps()
end
@required_fields ~w(username pass)
@optional_fields ~w()
@doc """
Builds a changeset based on the `struct` and `params`.
"""
def changeset(struct, params \\ %{}) do
struct
|> cast(params, @required_fields, @optional_fields)
|> cast_assoc(:model_a)
|> validate_required([])
end
end
Model B exists, as I can get it via Repo.all(ModelB).
Model A changeset is successfully validated, and I can see the model_b_id value when I print model A changeset struct.
But when inserted, the reference is not inserted. Although I can see it when printing the changeset of model A, in the MySQL log, this field is completely missing, is not in the INSERT query.
I have played a little bit, and if I force this reference field to not be null in the MySQL table, then I get a {"does not exist", []}
for this foreign key field when inserting as a Repo.insert(...) response, although the model B exists in database.
I was supremely interested in a response to this question.
I'd lost >7 hours to the exact condition of "I KNOW this row was created, WHY OH WHY is the changeset not letting me store the reference?"
I thought I'd leave a suggestion for anybody with the same frustration to check your database migration references.
I was sure I had triple checked them, but there it was, clear as day after a good nights sleep. I had setup references to wrong table.
Hopefully this saves somebody some time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With