josefrichter

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!

al2o3cr

al2o3cr

The documentation for INSERT specifically calls out that the output_expression passed to RETURNING “can use any column names of the table named by table_name”.

My interpretation of that statement is that this isn’t possible - RETURNING doesn’t allow arbitrary SQL.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
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

Other popular topics Top

chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement