Search code examples
postgresqlelixirecto

Ecto migration: remove reference, keep field


In a previous migration, I defined:

create table(:my_table) do
  add :reference_id, references(:references), null: false
  (...)
end

Now I want to remove the reference and the null: false, but still keep the reference_id field.

So I want something like:

alter table(:my_table) do
  modify :reference_id, <SOME_TYPE>, null: true
end

My DB is Postgres, so I think should be :bigint.

I have two questions: - Is the above correct? - If I understand correctly, this migration cannot be rolled back directly, so I have to create up and down functions. If that code is in the up of my migration, what should go in the down?


Solution

  • bigint looks correct to me. id fields in PostgreSQL are defined to be of type bigint by Ecto by default.

    modify takes the same arguments as add, so your up code would be:

    modify :reference_id, :bigint, null: true
    

    and down would be:

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

    edit:

    with foreign key constraints, the down will not work unless you remove the foreign key constraint in the up like this:

    drop constraint(:my_table, "my_table_reference_id_fkey")