Sakthibalan

Sakthibalan

(KeyError) key :id not found in: %Ash.Error.Unknown

I am creating a custom read action to get students by :roll_number and :password.

defmodule MyApp.Students.Student do
  use Ash.Resource,
    domain: MyApp.Students,
    data_layer: AshPostgres.DataLayer,
    extensions: [
      AshJsonApi.Resource
    ]

  json_api do
    type "student"

    routes do
      base("/students")

      get(:read, route: ":id")
      index :read
      get(:get_by_roll, route: "by_roll")
    end
  end

  actions do
    read :read do
      primary? true
    end

    read :get_by_roll do
      argument :roll_number, :string, allow_nil?: false
      argument :password, :string, allow_nil?: false

      filter expr(roll_number == ^arg(:roll_number) && password == ^arg(:password))
    end
  end



  postgres do
    table "students"
    repo MyApp.Repo
  end

  attributes do
    attribute :id, :uuid do
      writable? false
      primary_key? true
      default &Ecto.UUID.generate/0
      allow_nil? false
      public? true
    end

    attribute :first_name, :string do
      allow_nil? false
      public? true
    end

    attribute :last_name, :string do
      allow_nil? false
      public? true
    end

    attribute :class, :string, public?: true
    attribute :section, :string, public?: true
    attribute :dob, :date, public?: true
    attribute :father_name, :string, public?: true
    attribute :mother_name, :string, public?: true
    attribute :roll_number, :string, public?: true
    attribute :password, :string, public?: true

    create_timestamp :inserted_at
    update_timestamp :updated_at
  end
end

API Endpoint call
localhost:4000/api/students/by_roll?roll_number=100&password=sakthibalan@100

Getting Error:

Exception:

    ** (KeyError) key :id not found in: %Ash.Error.Unknown{
      errors: [
        %Ash.Error.Unknown.UnknownError{
          error: "** (Postgrex.Error) ERROR 42883 (undefined_function) function ash_elixir_and(boolean, boolean) does not exist\n\n    query: SELECT s0.\"id\", s0.\"first_name\", s0.\"last_name\", s0.\"class\", s0.\"section\", s0.\"dob\", s0.\"father_name\", s0.\"mother_name\", s0.\"roll_number\", s0.\"password\", s0.\"inserted_at\", s0.\"updated_at\" FROM \"students\" AS s0 WHERE ((ash_elixir_and(s0.\"roll_number\"::text = $1::text, s0.\"password\"::text = $2::text)))\n\n    hint: No function matches the given name and argument types. You might need to add explicit type casts.",
          field: nil,
          splode: Ash.Error,
          bread_crumbs: [],
          vars: [],
          path: [],
          stacktrace: #Splode.Stacktrace<>,
          class: :unknown
        }
      ]
    }
        (ash_json_api 1.0.0) lib/ash_json_api/serializer.ex:111: AshJsonApi.Serializer.serialize_one_error/2

First Post!

zachdaniel

zachdaniel

Creator of Ash

The fact that we aren’t handling the error there is bad, please open an issue on ash_json_api. To resolve it, add ”ash-functions” to your installed extensions in your repo, and generate migrations.

EDIT: or use and instead of &&

Last Post!

Sakthibalan

Sakthibalan

Yes, I already included “ash-functions”.

It’s working good after did this changes EDIT: or use and instead of &&

Thank you!

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54006 488
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
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
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

We're in Beta

About us Mission Statement