prkhrkat

prkhrkat

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

First Post!

karlosmid

karlosmid

Hi!

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

Last Post!

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.

Where Next?

Popular in Questions Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement