Ecto查找field_id而不是field

问题描述 投票:0回答:1

我正在尝试使用GraphQL设置Phoenix API。

我的数据库中有一个Team表和一个Game表,如下所示:

  schema "teams" do
    field :name, :string
    has_many :games, LvdnApi.Fixtures.Game
    timestamps()
  end

  schema "games" do
    field :away_team_score, :integer
    field :competition, :string
    field :game_datetime, :utc_datetime
    field :home_team_score, :integer
    field :round, :string
    belongs_to :home_team, LvdnApi.Fixtures.Team
    belongs_to :away_team, LvdnApi.Fixtures.Team
    timestamps()
  end

  @doc false
  def changeset(game, attrs) do
    game
    |> cast(attrs, [:home_team_score, :away_team_score, :competition, :round, :game_datetime, :home_team, :away_team])
    |> validate_required([:competition, :round, :game_datetime, :home_team, :away_team])
    |> assoc_constraint(:home_team)
    |> assoc_constraint(:away_team)
  end

以下是我在Absinthe Schema中的对象:

  object :team do
    field :id, non_null(:id)
    field :name, non_null(:string)
  end

  object :game do
    field :id, non_null(:id)
    field :home_team, :team, resolve: assoc(:home_team)
    field :away_team, :team, resolve: assoc(:away_team)
    field :home_team_score, :integer
    field :away_team_score, :integer
    field :competition, non_null(:string)
    field :round, non_null(:string)
    field :game_datetime, non_null(:datetime)
  end

假设我想执行一个简单的GraphQL查询,如下所示:

{
  allGames{
    id
    homeTeam {
      id
    }
  }
}

我收到以下错误:

[error] #PID<0.517.0> running LvdnApiWeb.Endpoint (connection #PID<0.516.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: POST /api/v1/graphiql
** (exit) an exception was raised:
    ** (Postgrex.Error) ERROR 42703 (undefined_column) column g0.home_team_id does not exist

    query: SELECT g0."id", g0."away_team_score", g0."competition", g0."game_datetime", g0."home_team_score", g0."round", g0."home_team_id", g0."away_team_id", g0."inserted_at", g0."updated_at" FROM "games" AS g0

    hint: Perhaps you meant to reference the column "g0.home_team".
        (ecto_sql) lib/ecto/adapters/sql.ex:605: Ecto.Adapters.SQL.raise_sql_call_error/1
        (ecto_sql) lib/ecto/adapters/sql.ex:538: Ecto.Adapters.SQL.execute/5
        (ecto) lib/ecto/repo/queryable.ex:147: Ecto.Repo.Queryable.execute/4
        (ecto) lib/ecto/repo/queryable.ex:18: Ecto.Repo.Queryable.all/3
        (lvdn_api) lib/lvdn_api_web/resolvers/fixtures_resolvers.ex:17: LvdnApiWeb.FixturesResolver.all_games/3
        (absinthe) lib/absinthe/resolution.ex:209: Absinthe.Resolution.call/2
        (absinthe) lib/absinthe/phase/document/execution/resolution.ex:209: Absinthe.Phase.Document.Execution.Resolution.reduce_resolution/1
        (absinthe) lib/absinthe/phase/document/execution/resolution.ex:168: Absinthe.Phase.Document.Execution.Resolution.do_resolve_field/4
        (absinthe) lib/absinthe/phase/document/execution/resolution.ex:153: Absinthe.Phase.Document.Execution.Resolution.do_resolve_fields/6
        (absinthe) lib/absinthe/phase/document/execution/resolution.ex:72: Absinthe.Phase.Document.Execution.Resolution.walk_result/5
        (absinthe) lib/absinthe/phase/document/execution/resolution.ex:53: Absinthe.Phase.Document.Execution.Resolution.perform_resolution/3
        (absinthe) lib/absinthe/phase/document/execution/resolution.ex:24: Absinthe.Phase.Document.Execution.Resolution.resolve_current/3
        (absinthe) lib/absinthe/pipeline.ex:274: Absinthe.Pipeline.run_phase/3
        (absinthe_plug) lib/absinthe/plug.ex:421: Absinthe.Plug.run_query/4
        (absinthe_plug) lib/absinthe/plug.ex:247: Absinthe.Plug.call/2
        (phoenix) lib/phoenix/router/route.ex:39: Phoenix.Router.Route.call/2
        (phoenix) lib/phoenix/router.ex:275: Phoenix.Router.__call__/1
        (lvdn_api) lib/lvdn_api_web/endpoint.ex:1: LvdnApiWeb.Endpoint.plug_builder_call/2
        (lvdn_api) lib/plug/debugger.ex:122: LvdnApiWeb.Endpoint."call (overridable 3)"/2
        (lvdn_api) lib/lvdn_api_web/endpoint.ex:1: LvdnApiWeb.Endpoint.call/2

如你所见,Ecto寻找home_team_id,而Game表中的字段称为home_team

我可以更改哪些内容以查找正确的字段名称?

elixir graphql phoenix-framework ecto absinthe
1个回答
0
投票

我刚刚发现问题来自迁移,我没有在这里发帖(对不起!)

我有:

  def change do
    create table(:games) do
      add :home_team_score, :integer
      add :away_team_score, :integer
      add :competition, :string
      add :round, :string
      add :game_datetime, :utc_datetime
      add :home_team, references(:teams, on_delete: :nothing)
      add :away_team, references(:teams, on_delete: :nothing)

      timestamps()
    end

    create index(:games, [:home_team])
    create index(:games, [:away_team])
  end

正确的代码是:

def change do
    create table(:games) do
      add :home_team_score, :integer
      add :away_team_score, :integer
      add :competition, :string
      add :round, :string
      add :game_datetime, :utc_datetime
      add :home_team_id, references(:teams, on_delete: :nothing)
      add :away_team_id, references(:teams, on_delete: :nothing)

      timestamps()
    end

    create index(:games, [:home_team_id])
    create index(:games, [:away_team_id])
  end
© www.soinside.com 2019 - 2024. All rights reserved.