marick
I’m wondering if I understand the Ecto.insert :returning option. Here’s how I think it works when inserting a changeset (and possibly a schema struct - I haven’t checked):
- Regardless of
:returning, the input:datais returned, including virtual fields. - Regardless of
:returning, the primary key is always added to that result.
Is that right?
Here’s some code:
Enum.at(@changesets, 0)
|> Crit.Repo.insert(prefix: "demo", returning: false)
|> IO.inspect
The return value is this:
{:ok,
%Crit.Usables.ServiceGap{
__meta__: #Ecto.Schema.Metadata<:loaded, "demo", "service_gaps">,
end_date: nil,
gap: %Ecto.Datespan{
first: :unbound,
last: ~D[2012-12-12],
lower_inclusive: false,
upper_inclusive: false
},
id: 1236,
reason: "before animal was put in service",
start_date: ~D[2012-12-12],
timezone: nil
}}
This is the same result as happens with returning: true or returning: [:id].
(I picked a schema without timestamps, because it was the most convenient. I’m betting I could affect whether timestamps were returned with :returning. I’m nearly out of power. I’ll check if my understanding above is wrong.)
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
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
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
PostgreSQL does support
RETURNINGon a detailed level (MySQL doesn’t seem to support it).It could be informative to see the generated SQL through an
iexsession (example).So
RETURNINGworks - but look atinsert/2’s return type:{:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}i.e. it’s obliged to return the full schema struct anyway.Presumably the returned values are used while the remaining ones are the ones that were inserted in the first place (which seems to happen here).
delete/2doesn’t mention supporting it.update/2doesn’t seem to work despite the documentation mentioning it.marick
The raw SQL looks like this:
… for all of these cases:
I tried it with a nonsense value and did get an error:
This to look at the source.
LostKobrakai
Changesets already hold all the data needed to return you the inserted value.
:returningsimilar to:read_after_writesis only useful if your db has autogenerated values, defaults, triggers or such means to modify the value you’re persisting and you want to read it back, because what the changeset holds won’t match what the db stores.marick
The following appears to be true, and I’ll make a pull request against the documentation (better written than the below).
By default,
insertreceives only the new primary key (typically,:id) from the database. Thatidis merged with the data given toinsertto produce what is usually a copy of the data in the database. The only exception is if the database itself changes or creates fields (with, for example, triggers). Here arereturning’s options:true: asks the database to provide all the fields in the record (including ones whose value is already known. (This only works when the data comes from a schema struct.)a list of fields: specify fields to be provided (always in addition to the primary key).
false: the same as giving noreturningkeyword: only the new primary key is provided.peerreynders
And any fields that have
:read_after_writesset totrue- they are essentially treated the same as the autogenerated id.What the documentation isn’t explicit about (likely assumed by implication) is that the returned values will replace the original values in the changeset
:data.josevalim
Correct.