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