Elixir,仅限Ecto回滚时间戳

Elixir,仅限Ecto回滚时间戳,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,在其中一个创建的表中,我必须在创建表之后添加时间戳。我当前的迁移脚本看起来像 defmodule Database.Repo.Migrations.UpdateSubscriptionTableInsertedAt do use Ecto.Migration def up do alter table(:subscriptions) do timestamps() end end def down do alter table(:subscr

在其中一个创建的表中,我必须在创建表之后添加时间戳。我当前的迁移脚本看起来像

defmodule Database.Repo.Migrations.UpdateSubscriptionTableInsertedAt do
  use Ecto.Migration
   def up do
    alter table(:subscriptions) do
      timestamps()
    end
  end
  def down do
    alter table(:subscriptions) do
      timestamps()
    end
  end
end
这对迁移很有效,但在尝试回滚时会抛出一个错误

[info] == Running Database.Repo.Migrations.UpdateSubscriptionTableInsertedAt.down/0 forward
[info] alter table subscriptions
** (Postgrex.Error) ERROR 42701 (duplicate_column): column "inserted_at" of relation "subscriptions" already exists

你知道我该如何解决这个问题吗?

你告诉它对上下都做同样的事情。在这种情况下,您实际上可以只指定一个回调,如下所示

def change do
  alter table(:subscriptions) do
    timestamps()
  end
end
这在迁移时是正确的,但在执行回滚时也会删除字段。如果确实希望保持
向上/0
向下/0
回调,则需要手动删除这些字段

def down do
  alter table(:subscriptions) do
    remove :inserted_at
    remove :updated_at
  end
end

你告诉它对上下都做同样的事情。在这种情况下,您实际上可以只指定一个回调,如下所示

def change do
  alter table(:subscriptions) do
    timestamps()
  end
end
这在迁移时是正确的,但在执行回滚时也会删除字段。如果确实希望保持
向上/0
向下/0
回调,则需要手动删除这些字段

def down do
  alter table(:subscriptions) do
    remove :inserted_at
    remove :updated_at
  end
end

谢谢你的回答。我一读文档就明白了这一点。有没有一种方法可以在不指定字段名的情况下回滚?请执行第一个选项。谢谢您的回答。我一读文档就明白了这一点。有没有办法在不指定字段名的情况下回滚?请执行第一个选项。