need help, on how to do it returning updated fields ?

How can I return to Repo.update only those fields that have been updated so as not to return the entire structure.
example :

def changeset(struct, params) do
    struct
    |> cast(params, [:id, :title, :type, :description, :user_id, :workspaces_id, :inserted_at, :updated_at])
    |> validate_required([:id, :title, :description])
  end
def update_change(%{id: 240333852503965697, title: "title", description: "description"}) do

    params
        |> changeset(params)
        |> Repo.update()
  end

result return ->

%Database.Resources.MyApp{
  deleted_at: nil,
  description: "description",
  id: 240333852503965697,
  inserted_at: ~U[2020-03-13 12:04:32Z],
  title: "title",
  updated_at: ~U[2020-03-13 12:59:05Z],
  user_id: "777f47cb-28af-4b87-a45a-a3368e151190",
  workspaces_id: "c9e32ee2-2620-4db7-ac3a-6c30ed5b0f87"
}

I need return only field have been updated
example :

%Database.Resources.MyApp{
  description: "description",
  id: 240333852503965697,
  title: "title",
  updated_at: ~U[2020-03-13 12:59:05Z],
}

From the changeset retrieve fields which are actually updated and then use the :returning option with Repo.update/2 - https://hexdocs.pm/ecto/Ecto.Repo.html#c:update/2

The intent “return only some fields” doesn’t match up smoothly with “return a struct”, since structs always have all their declared fields. What’s your goal?

2 Likes

To be clear, it isn’t re-fetching all fields. It’s just keeping the ones that were in memory and merging the ones that you specify via returning.