galbangi

galbangi

i am trying to update record. its giving me error. on insert its working fine.

[error] ** (Ecto.NoPrimaryKeyValueError) struct %ShopRestApis.Schema.CustomerSchema{__meta__: #Ecto.Schema.Metadata<:built, "customer">, id: nil, first_name: nil, last_name: nil, address: nil, landmark: nil, city: nil, state: nil, postcode: nil, type: nil, status: nil, mobile: nil, phone: nil, email: nil, company_name: nil, tax_code: nil, jobs: nil, inserted_at: nil, updated_at: nil} is missing primary key value
(ecto 3.12.2) lib/ecto/repo/schema.ex:1078: anonymous fn/3 in Ecto.Repo.Schema.add_pk_filter!/2

but i have id in changes

[(shop_rest_apis 0.1.0) lib/shop_rest_apis/context/customer_context.ex:67: ShopRestApis.Context.CustomerContext.update/2]
value #=> #Ecto.Changeset<
  action: nil,
  changes: %{
    id: "27efc595-dba9-4d9a-8a16-a4fd0d733c17",
    status: "Active",
    type: "Private",
    mobile: "+39,350577777",
    first_name: "test"
  },
  errors: [],
  data: #ShopRestApis.Schema.CustomerSchema<>,
  valid?: true,
  ...
>

my update function

  def update(%CustomerSchema{} = data, attrs) do
    data
    |> CustomerSchema.changeset(attrs)
    |> Repo.update()
  end

controller

def update(conn, params) do
    customer =
      ContextAliasController.get!(params["id"])

    with {:ok, %SchemaAliasController{} = customer} <-
           ContextAliasController.update(
             customer,
             params
           ) do
      render(conn, :show, customer: customer)
    end
  end

on schema i have uuid as primary key

  @primary_key {:id, :binary_id, autogenerate: false}
  @foreign_key_type :binary_id

Showing Posts 1 to 4

dimitarvp

dimitarvp

Can you show the CustomerSchema.changeset function? Maybe also ShopRestApis.Context.CustomerContext.update?

galbangi

galbangi OP

defmodule ShopRestApis.Schema.CustomerSchema do
  use Ecto.Schema
  import Ecto.Changeset

  @fields [
    :id,
    :first_name,
    :last_name,
    :address,
    :landmark,
    :city,
    :state,
    :postcode,
    :mobile,
    :phone,
    :email,
    :company_name,
    :tax_code,
    :type,
    :status,
    :jobs,
    :shop_id
  ]
  @validation_fields [:first_name, :mobile, :type, :status]

  @primary_key {:id, :binary_id, autogenerate: false, read_after_writes: true}
  @foreign_key_type :binary_id
  schema "customer" do
    field :first_name, :string
    field :last_name, :string
    field :address, :string
    field :landmark, :string
    field :city, :string
    field :state, :string
    field :postcode, :string
    field :type, :string
    field :status, :string
    field :mobile, :string
    field :phone, :string
    field :email, :string
    field :company_name, :string
    field :tax_code, :string
    field :jobs, :integer

    belongs_to :shop, ShopRestApis.Schema.ShopSchema,
      foreign_key: :shop_id,
      type: @foreign_key_type

    timestamps(type: :utc_datetime)
  end

  @doc false
  def changeset(_, attrs) do
    %__MODULE__{}
    |> cast(attrs, @fields)
    |> validate_required(@validation_fields)
  end
end

update function

  def update(%CustomerSchema{} = data, attrs) do
    data
    |> CustomerSchema.changeset(attrs)
    |> Repo.update_all()
  end
al2o3cr

al2o3cr

Update isn’t going to work unless you use the first argument to changeset :stuck_out_tongue:

galbangi

galbangi OP

i dont know what to say. stupid mistake. thank you very much.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Hellowman
Hello everyone! I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++. Does anyone ...
New
yarrichar
I’m very new to Elixir, so sorry if this is a silly question: I am wondering what happens to the mailbox of a process when the machine i...
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
Fl4m3Ph03n1x
Background When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error: * assembling mark...
New
mandar1045
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
saleyn
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
261 13183 112
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
charlenopires
Hi everyone :waving_hand: I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews