Elixir 添加对现有列和表的引用

Elixir 添加对现有列和表的引用,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,以下是我的原始迁移: def change do create table(:clipboard_items) do add(:clipboard_id, :integer) timestamps() end end 在这次迁移过程中,我忘记为每个字段添加引用。应该是这样的: def change do create table(:clipboard_items) do add(:clipboard_id, references(:clipboards))

以下是我的原始迁移:

def change do
  create table(:clipboard_items) do
    add(:clipboard_id, :integer)

    timestamps()
  end
end
在这次迁移过程中,我忘记为每个字段添加引用。应该是这样的:

def change do
  create table(:clipboard_items) do
    add(:clipboard_id, references(:clipboards))

    timestamps()
  end
end
如何将引用添加到现有表中

我试过这个:

  def change do
    alter table(:clipboard_items) do
      modify :clipboard_id, references(:clipboards)
    end
  end
但我得到了这个错误:

alter table clipboard_items
** (Postgrex.Error) ERROR 42710 (duplicate_object) constraint "clipboard_items_clipboard_id_fkey" for relation "clipboard_items" already exists

这是使我不能级联删除,任何帮助这将是伟大的

结果表明,这将起作用:

defmodule Dreamhouse.Repo.Migrations.AddReferenceToClipboardItem do
  use Ecto.Migration

  def up do
    execute "ALTER TABLE clipboard_items DROP CONSTRAINT clipboard_items_clipboard_id_fkey"

    alter table(:clipboard_items) do
      modify :clipboard_id, references(:clipboards, on_delete: :delete_all)
    end
  end

  def down do
    execute "ALTER TABLE clipboard_items DROP CONSTRAINT clipboard_items_clipboard_id_fkey"

    alter table(:clipboard_items) do
      modify :clipboard_id, references(:clipboards, on_delete: :nothing)
    end
  end
end