How to use empty_values about cast/4?

I defined a schema like this:

  @primary_key {:address, :string, autogenerate: false}
  schema "validators" do
    field :name, :string
    field :logo, :string
    field :status, :integer

and I has inserted a record to DB table:

0x01	myname	test_log	0	2024-01-19 03:30:54	2024-01-19 07:14:25

Now I intend to update this record like this, and I hope the other columns: “logo”, “status” to be set to defaut value.

update validators set name = "new_name" where address = "0x01"

I noticed the cast/4 function has a empty_values option, so, does this option can do this for me? and how to do?


Changesets are a higher level abstraction on applying changes then what this query does. Changesets require knowledge about the existing record to be able to calculate what changed and apply those changes to the database.

You can however use Repo.update_all to accomplish the query you posted more directly:

import Ecto.Query
query = from v in "validators", where: v.address = "0x01"
Repo.update_all(query, set: [name: "new_name"])
1 Like