Ecto attempting to insert a second record with zero data

I am attempting to create two records at once in my database: An adventure and many acts, where an adventure has many acts.

For some reason when I create an adventure it seemingly inserts one of the acts and then tries to insert a second adventure?

INSERT INTO "adventures" ("agent_person_id","contract_person_id","plot","type","world_id","inserted_at","updated_at","id") VALUES ($1,$2,$3,$4,$5,$6,$7,$8) [<<68, 147, 149, 173, 180, 173, 75, 0, 180, 244, 90, 191, 153, 186, 178, 215>>, <<186, 63, 34, 237, 155, 136, 68, 155, 170, 252, 3, 181, 208, 228, 239, 54>>, "placeholder", "placeholder", <<130, 112, 39, 250, 101, 217, 66, 162, 174, 235, 144, 23, 17, 78, 226, 167>>, ~N[2022-01-16 04:41:54], ~N[2022-01-16 04:41:54], <<233, 125, 1, 196, 52, 201, 79, 78, 140, 135, 179, 22, 224, 137, 5, 213>>]
[debug] QUERY OK db=1.2ms
INSERT INTO "acts" ("adventure_id","world_id","inserted_at","updated_at","id") VALUES ($1,$2,$3,$4,$5) [<<233, 125, 1, 196, 52, 201, 79, 78, 140, 135, 179, 22, 224, 137, 5, 213>>, <<130, 112, 39, 250, 101, 217, 66, 162, 174, 235, 144, 23, 17, 78, 226, 167>>, ~N[2022-01-16 04:41:54], ~N[2022-01-16 04:41:54], <<151, 150, 215, 195, 161, 77, 74, 157, 135, 136, 163, 202, 35, 132, 75, 203>>]
[debug] QUERY ERROR db=7.9ms
INSERT INTO "adventures" ("inserted_at","updated_at","id") VALUES ($1,$2,$3) [~N[2022-01-16 04:41:54], ~N[2022-01-16 04:41:54], <<218, 158, 22, 55, 104, 91, 64, 125, 143, 216, 110, 121, 85, 170, 53, 210>>]
[debug] QUERY OK db=0.6ms
rollback []

As you can see the adventure I tried to create successfully inserted into the database. Then the first of the acts gets inserted, then finally an error as an invalid adventure gets inserted.

I have checked all the locations where I call Repo.insert() and have accounted for all of them. The stacktrace I get for this error is a bunch of macro gibberish:

** (Postgrex.Error) ERROR 23502 (not_null_violation) null value in column "plot" of relation "adventures" violates not-null constraint

    table: adventures
    column: plot

Failing row contains (da9e1637-685b-407d-8fd8-6e7955aa35d2, null, null, null, null, null, 2022-01-16 04:41:54, 2022-01-16 04:41:54).
    (ecto_sql 3.7.1) lib/ecto/adapters/sql.ex:760: Ecto.Adapters.SQL.raise_sql_call_error/1
    (ecto 3.7.1) lib/ecto/repo/schema.ex:744: Ecto.Repo.Schema.apply/4
    (ecto 3.7.1) lib/ecto/repo/schema.ex:367: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
    (ecto 3.7.1) lib/ecto/association.ex:1083: Ecto.Association.BelongsTo.on_repo_change/5
    (ecto 3.7.1) lib/ecto/association.ex:554: Ecto.Association.on_repo_change/7
    (elixir 1.13.0) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.7.1) lib/ecto/association.ex:532: Ecto.Association.on_repo_change/4
    (ecto 3.7.1) lib/ecto/repo/schema.ex:873: Ecto.Repo.Schema.process_parents/5
    (ecto 3.7.1) lib/ecto/repo/schema.ex:347: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
    (ecto 3.7.1) lib/ecto/association.ex:1083: Ecto.Association.BelongsTo.on_repo_change/5
    (ecto 3.7.1) lib/ecto/association.ex:554: Ecto.Association.on_repo_change/7
    (elixir 1.13.0) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.7.1) lib/ecto/association.ex:532: Ecto.Association.on_repo_change/4
    (ecto 3.7.1) lib/ecto/repo/schema.ex:873: Ecto.Repo.Schema.process_parents/5
    (ecto 3.7.1) lib/ecto/repo/schema.ex:347: anonymous fn/15 in Ecto.Repo.Schema.do_insert/4
    (ecto 3.7.1) lib/ecto/association.ex:814: Ecto.Association.Has.on_repo_change/5
    (ecto 3.7.1) lib/ecto/association.ex:572: anonymous fn/8 in Ecto.Association.on_repo_change/7
    (elixir 1.13.0) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.7.1) lib/ecto/association.ex:568: Ecto.Association.on_repo_change/7
    (elixir 1.13.0) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3

I can provide as much detail as needed

Post the relevant Elixir code, please?

1 Like

Are you using put_assoc or cast_assoc anywhere? My hunch is in the Act schema you are using one of these.

I am in fact using cast_assoc in adventures and act. Adventures cast_assoc Acts and Acts cast_assoc a third relationship

Post the relevant Elixir code, please?

1 Like