मेरे पास एक 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

मैं बीज डेटा रखना चाहता हूं इसलिए मैंने इसे seeds.ex में लिखा है:

alias LvdnApi.Repo
alias LvdnApi.Fixtures.Team
alias LvdnApi.Fixtures.Game

%Team{name: "Olympique Lyonnais"} |> Repo.insert!
%Team{name: "Stade de Reims"} |> Repo.insert!
%Team{name: "Lille OSC"} |> Repo.insert!

%Game{
  home_team: 1,
  away_team: 2,
  home_team_score: 2,
  away_team_score: 2,
  competition: "Ligue 1",
  round: "14ème journée",
  game_datetime: %DateTime{
    year: 2018,
    month: 11,
    day: 24,
    zone_abbr: "CET",
    hour: 20,
    minute: 0,
    second: 0,
    microsecond: {0, 0},
    utc_offset: 3600,
    std_offset: 0,
    time_zone: "Etc/UTC"
  }
}
|> Repo.insert!

%Game{
  home_team: 3,
  away_team: 1,
  competition: "Ligue 1",
  round: "35ème journée",
  game_datetime: %DateTime{
    year: 2019,
    month: 5,
    day: 5,
    zone_abbr: "CET",
    hour: 21,
    minute: 0,
    second: 0,
    microsecond: {0, 0},
    utc_offset: 3600,
    std_offset: 0,
    time_zone: "Etc/UTC"
  }
}
|> Repo.insert!

यह मेरे Teams के लिए ठीक काम करता है लेकिन मुझे यह त्रुटि मेरे Games के लिए मिलती है:

* (Ecto.InvalidChangesetError) could not perform insert because changeset is invalid.

Errors

    %{away_team: [{"is invalid", [type: :map]}]}

Applied changes

    %{
      away_team_score: 2,
      competition: "Ligue 1",
      game_datetime: #DateTime<2018-11-24 20:00:00+01:00 CET Etc/UTC>,
      home_team_score: 2,
      round: "14ème journée"
    }

Params

    nil

Changeset

    #Ecto.Changeset<
      action: :insert,
      changes: %{
        away_team_score: 2,
        competition: "Ligue 1",
        game_datetime: #DateTime<2018-11-24 20:00:00+01:00 CET Etc/UTC>,
        home_team_score: 2,
        round: "14ème journée"
      },
      errors: [away_team: {"is invalid", [type: :map]}],
      data: #LvdnApi.Fixtures.Game<>,
      valid?: false
    >

    (ecto) lib/ecto/repo/schema.ex:169: Ecto.Repo.Schema.insert!/4
    priv/repo/seeds.exs:42: (file)
    (elixir) lib/code.ex:767: Code.require_file/2
    (mix) lib/mix/tasks/run.ex:147: Mix.Tasks.Run.run/5
    (mix) lib/mix/tasks/run.ex:86: Mix.Tasks.Run.run/1
    (mix) lib/mix/task.ex:331: Mix.Task.run_task/3
    (mix) lib/mix/task.ex:365: Mix.Task.run_alias/3
    (mix) lib/mix/task.ex:292: Mix.Task.run/2
    (mix) lib/mix/task.ex:365: Mix.Task.run_alias/3
    (mix) lib/mix/task.ex:292: Mix.Task.run/2
    (mix) lib/mix/cli.ex:79: Mix.CLI.run_task/2
    (elixir) lib/code.ex:767: Code.require_file/2

मुझे गेम स्ट्रक्चर कैसे लिखना चाहिए ताकि मैं उन्हें सही तरीके से बना सकूं?

0
Uj Corb 20 अप्रैल 2019, 11:12

1 उत्तर

सबसे बढ़िया उत्तर

पता चला कि जब मैं Seeds.ex में गेम बनाता हूं तो मुझे home_team और away_team को home_team_id और away_team_id में बदलना पड़ता है।

%Game{
  home_team_id: 1,
  away_team_id: 2,
  home_team_score: 2,
  away_team_score: 2,
  competition: "Ligue 1",
  round: "14ème journée",
  game_datetime: %DateTime{
    year: 2018,
    month: 11,
    day: 24,
    zone_abbr: "CET",
    hour: 20,
    minute: 0,
    second: 0,
    microsecond: {0, 0},
    utc_offset: 3600,
    std_offset: 0,
    time_zone: "Etc/UTC"
  }
}
|> Repo.insert!
0
Uj Corb 20 अप्रैल 2019, 10:05