Elixir,Ecto 仅回滚时间戳
Elixir, Ecto rollback only timestamp
在创建的 table 之一中,我必须在创建 table 之后添加时间戳。我当前的迁移脚本看起来像
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
这在迁移时会做正确的事情,但在回滚时也会删除字段。如果您真的想保留 up/0
和 down/0
回调,则需要手动删除这些字段。
def down do
alter table(:subscriptions) do
remove :inserted_at
remove :updated_at
end
end
在创建的 table 之一中,我必须在创建 table 之后添加时间戳。我当前的迁移脚本看起来像
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
这在迁移时会做正确的事情,但在回滚时也会删除字段。如果您真的想保留 up/0
和 down/0
回调,则需要手动删除这些字段。
def down do
alter table(:subscriptions) do
remove :inserted_at
remove :updated_at
end
end