Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 这个外部约束错误想告诉我什么?_Elixir_Phoenix Framework_Ecto - Fatal编程技术网

Elixir 这个外部约束错误想告诉我什么?

Elixir 这个外部约束错误想告诉我什么?,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,以下是我的模式和迁移: ** (Ecto.ConstraintError) constraint error when attempting to insert model: * foreign_key: matches_person_id_fkey 调用Repo.insert(变更集)时使用的变更集是: 此错误告诉您id为55的人不存在 有些相关,在迁移过程中,不需要以下内容: ... Repo.transaction(fn -> case Repo.insert(per

以下是我的模式和迁移:

** (Ecto.ConstraintError) constraint error when attempting to insert model:

    * foreign_key: matches_person_id_fkey
调用Repo.insert(变更集)时使用的变更集是:


此错误告诉您id为55的人不存在

有些相关,在迁移过程中,不需要以下内容:

...
Repo.transaction(fn ->
  case Repo.insert(person_changeset) do
  {:ok, person} ->
     Match.create(person.id)
  {:error, _}
     Repo.rollback(:no_bueno)
  end
end)

def create(person_id) do
  ...
  params = %{"person_id" => person.id, "percentage" => percentage, "star_id" => star.id}
  changeset = __MODULE__.changeset(%__MODULE__{}, params)
  case Repo.insert(changeset) do
  {:ok, _person} ->
     IO.puts "success"
  {:error, _changeset} ->
     Repo.rollback(:no_bueno)
  end
end
当您执行以下操作时,将自动创建索引:

如果要填充变更集中的错误,需要将以下内容添加到变更集中:

add :person_id, references(:persons)
虽然与问题没有直接关系,但看起来您可能在模型中使用了
Repo
。请看

...
Repo.transaction(fn ->
  case Repo.insert(person_changeset) do
  {:ok, person} ->
     Match.create(person.id)
  {:error, _}
     Repo.rollback(:no_bueno)
  end
end)

def create(person_id) do
  ...
  params = %{"person_id" => person.id, "percentage" => percentage, "star_id" => star.id}
  changeset = __MODULE__.changeset(%__MODULE__{}, params)
  case Repo.insert(changeset) do
  {:ok, _person} ->
     IO.puts "success"
  {:error, _changeset} ->
     Repo.rollback(:no_bueno)
  end
end
create index(:matches, [:person_id])
add :person_id, references(:persons)
cast(match, params, ~w(person_id), ~w())
|> foreign_key_constraint(:person_id)