代码之家  ›  专栏  ›  技术社区  ›  Alan

异位迁移:删除引用,保留字段

  •  1
  • Alan  · 技术社区  · 6 年前

    在上一次迁移中,我定义了:

    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
    

    我的DB是Postgres,所以我想应该是:bigint。

    我有两个问题: -以上是否正确? -如果我理解正确,则无法直接回滚此迁移,因此我必须创建 up down 功能。如果该代码在 向上的 在我的迁移过程中 向下 ?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Kyle Mellander    6 年前

    bigint 在我看来是对的。 id PostgreSQL中的字段定义为 bigint公司 默认情况下,按CTO。

    modify 采用与相同的参数 add ,因此您的up代码为:

    modify :reference_id, :bigint, null: true
    

    下面是:

    modify :reference_id, references(:references), null: false
    

    编辑:

    对于外键约束,除非在中删除外键约束,否则向下键将不起作用 up 这样地:

    drop constraint(:my_table, "my_table_reference_id_fkey")