I am trying to test a relationship between a parent and a child.
The migration for the minor is the following one:
defmodule ReservationBook.Repo.Migrations.CreateMinors do
use Ecto.Migration
def change do
create table(:minors) do
add :name, :string, null: false
add :surname, :string, null: false
add :age, :integer, null: false
add :course, :string, null: false
add :user_id, references("users", on_delete: :delete_all), null: false
timestamps()
end
create index(:minors, [:user_id])
end
end
The struct if the following one:
defmodule ReservationBook.Attendees.Minor do
use Ecto.Schema
import Ecto.Changeset
schema "minors" do
field :age, :integer
field :course, :string
field :name, :string
field :surname, :string
belongs_to :user, ReservationBook.Accounts.User
timestamps()
end
@doc false
def changeset(minor, attrs) do
minor
|> cast(attrs, [:name, :surname, :age, :course, :user_id])
|> validate_required([:name, :surname, :age, :course])
|> foreign_key_constraint(:user_id)
end
end
My problem is the the foreign_key_constraint in the changeset. Even though I am expecting the code to return a changeset {:error, changeset}, what I get is an error in Postgres
** (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column "user_id" violates not-null constraint
table: minors
column: user_id
Failing row contains (538, Sergio, bifvcmpw qtvtos, 11, primary first, null, 2021-01-26 17:53:27, 2021-01-26 17:53:27).
code: result = Attendees.create_minor(attrs = valid_minor_attributes(%{}, [new_user: false]))
stacktrace:
(ecto_sql 3.5.4) lib/ecto/adapters/sql.ex:751: Ecto.Adapters.SQL.raise_sql_call_error/1
(ecto 3.5.6) lib/ecto/repo/schema.ex:649: Ecto.Repo.Schema.apply/4
(ecto 3.5.6) lib/ecto/repo/schema.ex:262: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
test/reservation_book/attendees_test.exs:39: (test)
I have tried with cast_assoc, and made many changes in the code but I always get the same error instead of the changeset I was expecting
What I am doing wrong?