prkhrkat

prkhrkat

Hey Guys,

I’m stuck on something and would appreciate some help.

I have a list of objects and want to insert all of them at once and revert all of them if anything is invalid. So I’ve written the following code-

        Ecto.Multi.new()
        |> Ecto.Multi.insert_all(:insert_all, Asset, assets)
        |> Medqcx.Repo.transaction()

where assets hold the list of objects and the object looks like this -

`

%{“device_model_id” => “87”, “identification_number” => “AMH-P-3”, “last_service_date” => “2022-03-22”, “location” => “OT”, “purchase_cost” => “1000”}

`

But it’s giving following error -

(Ecto.ChangeError) value `"155"` for `Medqcx.Schema.Asset.device_model_id` in `insert_all` does not match type :integer
    (ecto 3.10.2) lib/ecto/repo/schema.ex:1019: Ecto.Repo.Schema.dump_field!/6
    (ecto 3.10.2) lib/ecto/repo/schema.ex:191: Ecto.Repo.Schema.extract_value/6
    (elixir 1.14.4) lib/enum.ex:1786: anonymous fn/3 in Enum.map_reduce/3
    (stdlib 4.3.1) maps.erl:411: :maps.fold_1/3
    (elixir 1.14.4) lib/enum.ex:2480: Enum.map_reduce/3
    (ecto 3.10.2) lib/ecto/repo/schema.ex:87: anonymous fn/5 in Ecto.Repo.Schema.extract_header_and_fields/8
    (elixir 1.14.4) lib/enum.ex:1780: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (ecto 3.10.2) lib/ecto/repo/schema.ex:86: Ecto.Repo.Schema.extract_header_and_fields/8
    (ecto 3.10.2) lib/ecto/repo/schema.ex:48: Ecto.Repo.Schema.do_insert_all/7
    (ecto 3.10.2) lib/ecto/multi.ex:861: Ecto.Multi.apply_operation/4
    (ecto 3.10.2) lib/ecto/multi.ex:844: Ecto.Multi.apply_operation/5
    (elixir 1.14.4) lib/enum.ex:2468: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.10.2) lib/ecto/multi.ex:818: anonymous fn/5 in Ecto.Multi.apply_operations/5
    (ecto_sql 3.10.1) lib/ecto/adapters/sql.ex:1203: anonymous fn/3 in Ecto.Adapters.SQL.checkout_or_transaction/4
    (db_connection 2.5.0) lib/db_connection.ex:1630: DBConnection.run_transaction/4
    (ecto 3.10.2) lib/ecto/repo/transaction.ex:18: Ecto.Repo.Transaction.transaction/4
    (medqcx 0.1.0) lib/medqcx_web/helpers/asset_bulk_uploader.ex:108: Medqcx.Helper.AssetBulkUpload.dump_csv_file/3
    (medqcx 0.1.0) lib/medqcx_web/helpers/asset_bulk_uploader.ex:31: Medqcx.Helper.AssetBulkUpload.ingest_csv/3

When I insert the same through Repo.insert(), it goes through. But it does not solve my case.
What should I do?

Also providing my schema

 schema "assets" do
    field :serial_number, :string
    field :last_service_date, :date
    field :location, :string
    field :identification_number, :string
    field :purchase_date, :date
    field :purchase_cost, :integer
    belongs_to :device_model, DeviceModel, type: :integer
    timestamps()
  end

Showing Posts 1 to 2

karlosmid

karlosmid

Hi!

Data that you are inserting must have for device_model_id integers, not strings.

dimitarvp

dimitarvp

When you are using Repo.insert_all the data is not parsed / casted. So you have to make sure you are passing the right data – or have another processing step before inserting that will do the parsing / casting. You can use Ecto.Changeset or you can do it yourself.

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New
dillonoconnor
Is there any way to avoid the Hologram compiler running when using iex? It seems like the front-end code could potentially be disregarded...
New
thiagogsr
** (ArgumentError) expected :max_attempts to be a positive integer, got: {:@, [line: 10, column: 19], [{:max_attempts, [line: 10, column:...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews