Pistrie
Result of upsert has different binary_id, but this is corrected after the upsert?
So I have the following test:
Post #1 has a title and a body, and is present in the database.
Post #2 has the same title but a different body, and is not yet present in the database.
I have the following upsert logic:
%Post{}
|> Post.changeset(params)
|> Repo.insert(
conflict_target: [:title],
on_conflict: [
set:
params
|> Map.to_list()
]
)
|> IO.inspect()
I supply this upsert with Post #2, meaning that I expect Post #1 to be updated to ‘become’ Post #2.
When I pipe this Repo.Insert into IO.inspect I notice that the ID is different from the ID in Post #1. However, when I check the database there is only one record, and it’s in the state I expect it to be in, meaning that the upsert was succesful. When did this ID get corrected?
I hope my explanation makes sense, and thank you in advance ![]()
Marked As Solved
endoooo
in this section of insert/2 doc you can find some information about upsert caveats, including misalignment between the returned struct and database.
I think in your case, if you need the returned to mirror the database, your can use the read_after_writes: true in the schema or the returning: true option in the insert:
Specify
read_after_writes: truein your schema for choosing fields that are read from the database after every operation. Or passreturning: truetoinsertto read all fields back. (Note that it will only read from the database if at least one field is updated).
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









