Trolleger

Trolleger

Hi! I was reading the docs for Ecto.repo’s insert. Ecto.Repo — Ecto v3.14.0 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. When true, returns all fields, including those marked as load_in_query: false. When false, no extra fields are returned. It will always include all fields in read_after_writes as 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! :slight_smile: I can give more details and elaborate need be, I know this post isn’t the best formatted and all

Showing Posts 1 to 4

NobbZ

NobbZ

It is in your quote…

emphasisis mine.

Trolleger

Trolleger OP

I understand, but the :returning option is not explicitly set, is returning automatic or what’s going on ?

NobbZ

NobbZ

The way I read what I have requoted:

regardless of the :returning value, any autogenerated id and all fields in read_after_writes are always returned.

Trolleger

Trolleger OP

Oh I understand what’s being said now with “When false, no extra fields are returned. It will always include all fields in read_after_writes as well as any autogenerated id.” and how you reread/resaid it, I should have probably read a little bit harder in the beginning, I get it now! Thank you!

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
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
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews