Hi! I was reading the docs for Ecto.repo’s insert. Ecto.Repo — Ecto v3.13.5 but there was a specific part that stood out to me, I should say I am sort of new to elixir so this might be an issue of my lack of knowledge so if anyone could point me to resources to learn more (about related to this question) that would be good. So as I was saying
“Options
:returning- selects which fields to return. It accepts a list of fields to be returned from the database. Whentrue, returns all fields, including those marked asload_in_query: false. Whenfalse, no extra fields are returned. It will always include all fields inread_after_writesas well as any autogenerated id. Be aware that the fields returned from the database overwrite what was supplied by the user. Any field not returned by the database will be present with the original value supplied by the user. Not all databases support this option and it may not be available during upserts. See the “Upserts” section for more information.”
So I suppose I do understand to a good enough extent how the options do work, after some quick research but I have a question about the :returning option. I have this code
case Accounts.create_user(user_params) do
# Delegates it to the Accounts module to insert the user in
{:ok, user} ->
# It runs the create user function with the user details, the create user function does %User and returns it back as the returned value
# from the function which is where the User comes from in {:ok, user} ->, the ok comes from
conn
# Status code 201 showing created
|> put_status(:created)
|> json(%{id: user.id, email: user.email, username: user.username})
with
def create_user(attrs) do
%User{}
|> User.registration_changeset(attrs)
|> Repo.insert()
end
As you can see here there is nothing here or in the code (as far as I know) that gives :returning! but if I am to run IO.inspect on the returned struct from the repo.insert it pops up, even though that ID was created at insertion time, is returning something automatic or what? Oh yeah, and the following code and console outputs to show you:
{:ok, user} ->
IO.inspect(user) in console gives
%ChatApp.Accounts.User{
__meta__: #Ecto.Schema.Metadata<:loaded, "users">,
id: "9cca4411-564b-4925-97fc-ea3da74cd8d8",
email: "suprasad@gmail.com",
username: "suprasda",
hashed_password: "$2b$12$.6/3jzi6qCb3OC8K/5qTVuk0flRtF/pO/cTxKmMPV0u8U8iP19xm6",
provider: nil,
provider_uid: nil,
password: "suprasd@gmail.com1",
inserted_at: ~U[2025-12-19 16:17:46Z],
updated_at: ~U[2025-12-19 16:17:46Z]
}
So somehow the returned fields from insertion and whatever else are returned within user(from the create user function) WITHOUT the :returning option! Either I don’t understand repo, options or something else is going on. I’m just a little bit confused about the topic and other things in general so if I can get any details that would be greatly appreciated!
I can give more details and elaborate need be, I know this post isn’t the best formatted and all






















