josefrichter
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.
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #ai
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #metaprogramming










Showing Posts 1 to 2- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
al2o3cr
The documentation for
INSERTspecifically calls out that theoutput_expressionpassed toRETURNING“can use any column names of the table named bytable_name”.My interpretation of that statement is that this isn’t possible -
RETURNINGdoesn’t allow arbitrary SQL.josefrichter
Yes, my interpretation is similar. Although such a feature doesn’t sound inconceivable to me.