augnustin

augnustin

Ecto embed timestamps inserted_at updated

So I have this embed structure:

defmodule MyApp.Struct do
  use Ecto.Schema
  import Ecto.Changeset
  
  @primary_key false
  @derive Jason.Encoder
  embedded_schema do
    field(:name, :string)
    field(:completed_at, :utc_datetime)
    timestamps()
  end

end

I wanted to have inserted_at and updated_at fields set, so I added the timestamps() line.

It seems like the updated_at value works correctly, but the inserted_at keeps on being updated on each update, which is not what I want.

Here’s how I do the update, from the Holder struct:

defmodule MyApp.Holder do
  schema "holder" do
    embeds_one(:struct, MyApp.Struct, on_replace: :delete)
  end

  def save_struct(holder, struct_params) do
    holder
    |> change()
    |> put_embed(
      :struct,
      MyApp.Holder.changeset(
        holder.struct || %MyApp.Holder{},
        struct_params
      )
    )
    |> Repo.update()
  end

end

I thought this would have to do with put_embed vs cast_embed, but with cast_embed, I need to pass all struct_params, which is not what I want.

So I went for a manual solution:

    field(:inserted_at, :utc_datetime)
    field(:updated_at, :utc_datetime)
  def changeset(struct, params) do
    struct
    |> cast(params, [:name, :completed_at])
    |> cast(%{inserted_at: struct.inserted_at || Timex.now(), updated_at: Timex.now()}, [:inserted_at, :updated_at])
  end

But this is much less elegant.

What am I doing wrong? Or is this because of the embed thing?

Thanks

First Post!

wolfiton

wolfiton

Hi,

What I experienced so far inserted_at and updated_at are reserved by timestamps(). Try to use something else for those fields in the migration like last_modified last_inserted. Then you can naivedatetime for the fields directly in the database or have a look here on other options.

Last Post!

augnustin

augnustin

Yes you are correct.

I did change the name of the objects to un-specify those, and I might have confused myself.

Unfortunately it seems like I can’t edit it anymore. :frowning:

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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

Other popular topics Top

KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36654 110
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54921 245
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement