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

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews