josefrichter
Insert_all returning row_number?
I have query like this that returns records with a virtual field row_number
from e in Enrollment,
order_by: [desc: e.processed_at],
select: %{e | row_number: over(row_number(), partition_by: e.class_id, order_by: e.processed_at)}
I am also doing some bulk insert_all. This is how I get the newly inserted records:
Repo.insert_all(Enrollment, enrollments_attrs,
on_conflict: :nothing,
conflict_target: [:class_id, :user_id],
returning: true
)
I was wondering whether it is somehow possible to get the row_number already during the insertion? I would expect the returning option to resemble the select clause from the above query. But I just can’t figure out what the syntax would be.
To be honest, I’m not sure it’s even possible by looking at Postgres docs.
More simplified example would be a simple insert with returning clause that contains any “calculated” parameter like avg.
Postgres docs for RETURNING is kinda sparse: PostgreSQL: Documentation: 18: 6.4. Returning Data from Modified Rows
I was also looking at postgres WITH that can be combined with UPDATE or INSERT, see PostgreSQL: Documentation: 18: 7.8. WITH Queries (Common Table Expressions)
Do you think it’s even possible? Thank you.
First Post!
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










