Ecto up and down not working

Hi all,

I believe that I have this set up correctly but to be honest, despite reading through the docs on up and down it still doesn’t make much sense. All I want to do is remove a not null constraint and the up down migration method seems like a very cumbersome way to do so.

Here is the original migration:

defmodule Gimli.Repo.Migrations.AddPostalCodesTable do
  use Ecto.Migration

  def change do
    create table(:postal_codes) do
      add(:postal_code, :string, null: false)
      add(:state_id, references(:states), null: false)
      add(:new_city_id, references(:new_cities), null: false)
      add(:county, :string)
      add(:population, :integer, null: false, default: 0)
      add(:lat, :float, null: false)
      add(:long, :float, null: false)

      timestamps()
    end

    create unique_index(:postal_codes, [:postal_code, :state_id])
  end
end

Here is what I have for my up down migration to remove the not null constraint on one field:

defmodule Gimli.Repo.Migrations.RemoveNullConstraintPostalcodetable do
  use Ecto.Migration

  def up do
    execute(
      "alter table postal_codes alter new_city_id drop not null"
    )
    alter table(:postal_codes) do
      modify(
        :new_city_id,
        references(:new_cities), 
        null: false
      )
    end
  end
  def down do
    execute(
      "alter table postal_codes alter new_city_id drop not null"
    )
    alter table(:postal_codes) do
      modify(
        :new_city_id,
        references(:new_cities), 
        null: true
      )
    end
  end
end

is there a simpler way to do this? If not could someone help me figure out why this migration is working?

This is the error it’s throwing:

(base) justinbenfit@MacBook-Pro-3 gimli % mix ecto.migrate

10:46:47.081 [info]  == Running 20211129171619 Gimli.Repo.Migrations.RemoveNullConstraintPostalcodetable.up/0 forward

10:46:47.090 [info]  execute "alter table postal_codes alter new_city_id drop not null"

10:46:47.095 [info]  alter table postal_codes
** (Postgrex.Error) ERROR 42710 (duplicate_object) constraint "postal_codes_new_city_id_fkey" for relation "postal_codes" already exists

what about like here: Can you modify a column's references/3 options in Ecto? - #10 by endoooo

try using from: and so you can just use change instead of up and down?

1 Like

Hi @maz Thanks for replying! It’s not working for some reason. This is what I’m trying and it’s throwing the same error as before:

defmodule Gimli.Repo.Migrations.Removenullconstraintpostalcodes do
  use Ecto.Migration

  def change do
    alter table(:postal_codes) do
      modify :new_city_id, references(:new_cities), null: false,
        from: [references(:new_cities), null: true]
    end
  end
end

Can you see anything that looks off? Thanks!

Oddly if I remove the brackets from the from: [references(:new_cities), null: true] making it from: references(:new_cities), null: true

then the migration migrates just fine but in the database the not null constraint doesn’t change.

This solved it for some reason!

defmodule Gimli.Repo.Migrations.Removenullconstraintpostalcodes do
  use Ecto.Migration

  def change do
    alter table(:postal_codes) do
      modify(:new_city_id, references(:new_cities), null: true, from: references(:new_cities))
    end
  end
end
1 Like