Postgresql 异位迁移:删除引用,保留字段
在上一次迁移中,我定义了:Postgresql 异位迁移:删除引用,保留字段,postgresql,elixir,ecto,Postgresql,Elixir,Ecto,在上一次迁移中,我定义了: create table(:my_table) do add :reference_id, references(:references), null: false (...) end 现在我想删除引用和null:false,但仍然保留reference\u id字段 所以我想要这样的东西: alter table(:my_table) do modify :reference_id, <SOME_TYPE>, null: true end
create table(:my_table) do
add :reference_id, references(:references), null: false
(...)
end
现在我想删除引用和null:false
,但仍然保留reference\u id
字段
所以我想要这样的东西:
alter table(:my_table) do
modify :reference_id, <SOME_TYPE>, null: true
end
alter table(:my_table)do
modify:reference\u id,null:true
结束
我的DB是Postgres,所以我认为应该是:bigint
我有两个问题:
-以上是正确的吗?
-如果理解正确,此迁移无法直接回滚,因此我必须创建
up
和down
函数。如果该代码在我的迁移的向上
中,那么向下
中应该包含什么?bigint
在我看来是正确的<默认情况下,Ecto将PostgreSQL中的code>id字段定义为bigint
类型
modify
采用与add
相同的参数,因此您的up代码为:
modify :reference_id, :bigint, null: true
modify :reference_id, references(:references), null: false
下面是:
modify :reference_id, :bigint, null: true
modify :reference_id, references(:references), null: false
编辑:
使用外键约束时,向下键将不起作用,除非您删除向上
中的外键约束,如下所示:
drop约束(:my\u table,“my\u table\u reference\u id\u fkey”)