Elixir graphql api error message: (Postgrex.Error) ERROR 42P01 (undefined_table) relation "items" does not exist

Why am I getting these errors when writing my values ​​to the localhost:4000 portal?

schema.ex

defmodule Project1Web.Schema do

  use Absinthe.Schema

  alias Project1.{Menu, Repo}

  query do

    field :menu_items, list_of(:menu_item) do

    resolve fn _, _, _ ->

    {:ok, Repo.all(Menu.Item)}

    end

    end

    end

  object :menu_item do

    field :id, :id

    field :name, :string

    field :description, :string

  end

end

menu/ıtem.ex

defmodule Project1.Menu.Item do

  use Ecto.Schema

  import Ecto.Changeset

  alias Project1.Menu.Item

  schema "items" do

  field :added_on, :date

  field :description, :string

  field :name, :string

field :price, :decimal

belongs_to :category, Project1.Menu.Category

many_to_many :tags, Project1.Menu.ItemTag,

join_through: "items_taggings"

timestamps()

end

@doc false

def changeset(%Item{} = item, attrs) do

item

|> cast(attrs, [:name, :description, :price, :added_on])

|> validate_required([:name, :price])

|> foreign_key_constraint(:category)

end

end

errors in terminal


[debug] QUERY ERROR source="items" db=0.0ms queue=21.0ms idle=847.5ms
SELECT i0."id", i0."added_on", i0."description", i0."name", i0."price", i0."category_id", i0."inserted_at", i0."updated_at" FROM "items" AS i0 []   
Ôå│ anonymous fn/3 in Project1Web.Schema.__absinthe_function__/2, at: lib/project1_web/schema.ex#9
[info] Sent 500 in 56ms
[error] #PID<0.543.0> running Project1Web.Endpoint (connection #PID<0.523.0>, stream id 5) terminated
Server: localhost:4000 (http)
Request: POST /api/graphiql
** (exit) an exception was raised:
    ** (Postgrex.Error) ERROR 42P01 (undefined_table) relation "items" does not exist

    query: SELECT i0."id", i0."added_on", i0."description", i0."name", i0."price", i0."category_id", i0."inserted_at", i0."updated_at" FROM "items" AS i0
        (ecto_sql 3.8.0) lib/ecto/adapters/sql.ex:928: Ecto.Adapters.SQL.raise_sql_call_error/1
        (ecto_sql 3.8.0) lib/ecto/adapters/sql.ex:843: Ecto.Adapters.SQL.execute/6
        (ecto 3.8.3) lib/ecto/repo/queryable.ex:221: Ecto.Repo.Queryable.execute/4
        (ecto 3.8.3) lib/ecto/repo/queryable.ex:19: Ecto.Repo.Queryable.all/3
        (project1 0.1.0) lib/project1_web/schema.ex:9: anonymous fn/3 in Project1Web.Schema.__absinthe_function__/2
        (absinthe 1.7.0) lib/absinthe/resolution.ex:209: Absinthe.Resolution.call/2
        (absinthe 1.7.0) lib/absinthe/phase/document/execution/resolution.ex:230: Absinthe.Phase.Document.Execution.Resolution.reduce_resolution/1  
        (absinthe 1.7.0) lib/absinthe/phase/document/execution/resolution.ex:185: Absinthe.Phase.Document.Execution.Resolution.do_resolve_field/3   
        (absinthe 1.7.0) lib/absinthe/phase/document/execution/resolution.ex:170: Absinthe.Phase.Document.Execution.Resolution.do_resolve_fields/6  
        (absinthe 1.7.0) lib/absinthe/phase/document/execution/resolution.ex:88: Absinthe.Phase.Document.Execution.Resolution.walk_result/5
        (absinthe 1.7.0) lib/absinthe/phase/document/execution/resolution.ex:67: Absinthe.Phase.Document.Execution.Resolution.perform_resolution/3  
        (absinthe 1.7.0) lib/absinthe/phase/document/execution/resolution.ex:24: Absinthe.Phase.Document.Execution.Resolution.resolve_current/3     
        (absinthe 1.7.0) lib/absinthe/pipeline.ex:384: Absinthe.Pipeline.run_phase/3
        (absinthe_plug 1.5.8) lib/absinthe/plug.ex:536: Absinthe.Plug.run_query/4
        (absinthe_plug 1.5.8) lib/absinthe/plug.ex:290: Absinthe.Plug.call/2
        (phoenix 1.6.9) lib/phoenix/router/route.ex:41: Phoenix.Router.Route.call/2
        (phoenix 1.6.9) lib/phoenix/router.ex:354: Phoenix.Router.__call__/2
        (project1 0.1.0) lib/project1_web/endpoint.ex:1: Project1Web.Endpoint.plug_builder_call/2
        (project1 0.1.0) lib/plug/debugger.ex:136: Project1Web.Endpoint."call (overridable 3)"/2
        (project1 0.1.0) lib/project1_web/endpoint.ex:1: Project1Web.Endpoint.call/2

Your schema specifies that it uses a table named items, but this error means there isn’t one of those in your database.

Have you run mix ecto:create and migrated?

2 Likes

C:\Users\baris\project1>mix ecto.create
The database for Project1.Repo has already been created

Created, but not migrated…

mix ecto.migrate

C:\Users\baris\project1>mix ecto.migrate

13:17:20.471 [info] Migrations already up

problems
invalid association category in schema Project1.Menu.Item: associated schema Project1.Menu.Category does not exist
invalid association tags in schema Project1.Menu.Item: associated schema Project1.Menu.ItemTag does not exist

Errors tell You miss some table/schemas.

Do You have Category, Tag and the join table between Item and Tag?

And related migrations…

I don’t have a table how can I create it

Hi @bar65 as I said in the other thread, the Absinthe book expects a basic level of familiarity with Ecto and Phoenix. If you don’t know how to create a table, you’re not going to be able to successfully get through the book. I would suggest starting with a more introductory tutorial on Phoenix or Ecto first.

3 Likes