外迁移:删除引用,保留字段
Ecto migration: remove reference, keep field
在之前的迁移中,我定义了:
create table(:my_table) do
add :reference_id, references(:references), null: false
(...)
end
现在我想删除引用和 null: false
,但仍保留 reference_id
字段。
所以我想要这样的东西:
alter table(:my_table) do
modify :reference_id, <SOME_TYPE>, null: true
end
我的数据库是Postgres,所以我觉得应该是:bigint。
我有两个问题:
- 以上是否正确?
- 如果我理解正确的话,这个迁移不能直接回滚,所以我必须创建 up
和 down
函数。如果该代码在我迁移的 up
中,那么 down
中应该包含什么?
bigint
在我看来是正确的。 id
PostgreSQL 中的字段默认被 Ecto 定义为 bigint
类型。
modify
采用与 add
相同的参数,因此您的上行代码将是:
modify :reference_id, :bigint, null: true
以下为:
modify :reference_id, references(:references), null: false
编辑:
使用外键约束,除非您像这样删除 up
中的外键约束,否则 down 将不起作用:
drop constraint(:my_table, "my_table_reference_id_fkey")
在之前的迁移中,我定义了:
create table(:my_table) do
add :reference_id, references(:references), null: false
(...)
end
现在我想删除引用和 null: false
,但仍保留 reference_id
字段。
所以我想要这样的东西:
alter table(:my_table) do
modify :reference_id, <SOME_TYPE>, null: true
end
我的数据库是Postgres,所以我觉得应该是:bigint。
我有两个问题:
- 以上是否正确?
- 如果我理解正确的话,这个迁移不能直接回滚,所以我必须创建 up
和 down
函数。如果该代码在我迁移的 up
中,那么 down
中应该包含什么?
bigint
在我看来是正确的。 id
PostgreSQL 中的字段默认被 Ecto 定义为 bigint
类型。
modify
采用与 add
相同的参数,因此您的上行代码将是:
modify :reference_id, :bigint, null: true
以下为:
modify :reference_id, references(:references), null: false
编辑:
使用外键约束,除非您像这样删除 up
中的外键约束,否则 down 将不起作用:
drop constraint(:my_table, "my_table_reference_id_fkey")