lundi 14 octobre 2019

Why am I getting this foreign key error in my phoenix tests?

I've got the following test in Phoenix:

test "list_orders/0 returns all orders" do
  {:ok, user} = %User{}
                |> User.changeset(@user_attrs)
                |> Repo.insert()

  changeset = %Order{}
              |> Order.changeset(@valid_attrs)
              |> Ecto.Changeset.put_change(:user_id, user.id)

  {:ok, order} = Repo.insert(changeset)

  assert MWS.list_orders() == [order]
end

Which is failing to insert changeset into the Repo because user_id is failing a foreign key constraint. But I'm using the user.id that's returned from the user successfully being inserted into the database?

Any idea what I'm doing wrong here?

Thanks!

Aucun commentaire:

Enregistrer un commentaire