Postgresql 为什么我不能在变更集中转换关联?
我正在尝试执行一个事务,替换唯一的位置值。当我重新插入节目时,即使我已将其与节目一起预装,并且我正在变更集中使用cast_assoc,但剧集也不会附加。以下是文件: 显示控制器.exPostgresql 为什么我不能在变更集中转换关联?,postgresql,elixir,phoenix-framework,ecto,Postgresql,Elixir,Phoenix Framework,Ecto,我正在尝试执行一个事务,替换唯一的位置值。当我重新插入节目时,即使我已将其与节目一起预装,并且我正在变更集中使用cast_assoc,但剧集也不会附加。以下是文件: 显示控制器.ex original_show = Repo.get!(Show, id) |> Repo.preload(:episodes) old_position = original_show.position %{"position" => new_position} = show_params new_po
original_show = Repo.get!(Show, id)
|> Repo.preload(:episodes)
old_position = original_show.position
%{"position" => new_position} = show_params
new_position = String.to_integer(new_position)
query = from(s in Show, where: s.position <= ^new_position, where: s.position > ^old_position)
shows = Repo.all(query) |> Repo.preload(:episodes) |> Enum.sort(&(&1.position < &2.position))
result = Repo.transaction(fn ->
Repo.delete!(original_show)
Repo.delete_all(query)
Enum.each shows, fn show ->
show = Map.put(show, :position, show.position - 1)
changeset = Show.changeset(show)
case Repo.insert(changeset) do
{:ok, show} -> show
{:error, changeset} -> Repo.rollback(changeset)
end
end
changeset = Show.changeset(original_show, new_show_params)
case Repo.insert(changeset) do
{:ok, show} -> show
{:error, changeset} -> Repo.rollback(changeset)
end
end)
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:title, :position, :shuffle_episodes])
|> cast_attachments(params, [:large_artwork_url, :parallax_artwork_url])
|> cast_assoc(:episodes)
|> validate_required([:title, :position, :large_artwork_url, :parallax_artwork_url, :shuffle_episodes])
|> unique_constraint(:position)
end
如果您将
changeset=Show.changeset(Show)
更改为changeset=Show.changeset(Show,%%{scents:Show.scents})
?@Dogbert我得到错误预期参数为映射,得到:%Test.scents{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuo.Schema.Metadata
@Dogbert我把剧集制作成了一张地图,现在我得到了你正试图改变关系:Test.Show的剧集,但是缺少数据。
@Dogbert在中添加了很多:剧集,Test.scene,在Show模式中的替换::nilify上,我现在得到了一个变更集错误saying错误:[显示id:{“不能为空”,[]}]