Trolleger

Trolleger

How do passing function options work; specifically with Repo

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

Marked As Solved

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.

Also Liked

NobbZ

NobbZ

It is in your quote…

emphasisis mine.

Trolleger

Trolleger

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!

Where Next?

Popular in Questions Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New

We're in Beta

About us Mission Statement