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.

Where Next?

Popular in Questions Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics Top

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
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement