Ecto.Repo.insert() vs Medqcx.Repo.transaction()

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

Hi!

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

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.