简体   繁体   中英

How to remove primary key column in ecto elixir

When I use remove key in migration it was not delete the constraint in the table it only delete column name. Please help how can i create for that migration file.

defmodule ControlCenter.Repo.Migrations.CreateRemedyTable do
use Ecto.Migration

def up do
  create table(:remedy, primary_key: false) do
     add :remedy_code, :string, primary_key: true
     add :description, :string
     add :cause_code, :string, primary_key: true

  end
 end

 def down do
    drop table(:remedy)
 end
end

I want to remove cause_code from the table

If you want to remove the column, you'll need to alter the table to remove the column after modifying it to not be part of the composite primary key:

alter table("remedy") do
  modify(:cause_code, primary_key: false)
  remove(:cause_code)
end

If you're deleting it for this migration, add it in the up function. If you're deleting it as a one-off, you can import Ecto.Query and do the above in iex -S mix . Keep in mind, there's not a good solution for migrating back down with a column removal, especially when it was part of a composite key.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM