1

I would like a database migration that is basically the following SQL:

ALTER TABLE `myTable` ADD COLUMN `id` INT AUTO_INCREMENT UNIQUE FIRST;

Is there a Sequel migration that does exactly this? I have gotten pretty close with the following, but it doesn't seem to be exactly what I want:

Sequel.migration do
  change do
    alter_table :my_table do
      add_column :id, Bignum, null: false, unique: true
    end
  end
end

Specifically, it seems to be missing auto-increment and won't be the first column.

4
  • 1
    Why does it matter if the id column is not the 1st one?
    – Shadow
    Commented Feb 24, 2016 at 0:17
  • Not critical that it is the 1st one, but if possible it would be nice. The auto-increment is critical. Also would like to backfill existing rows. Commented Feb 24, 2016 at 0:19
  • Shouldn't that be add_primary_key instead of add_column?
    – infused
    Commented Feb 24, 2016 at 0:36
  • add_primary_key makes an existing column a primary_key. I would like to add a new column that doesn't exist. Commented Feb 24, 2016 at 0:43

1 Answer 1

2

This should work:

DB.add_column :myTable, :id, Bignum, null: false, unique: true, :auto_increment=>true 

Note that that doesn't give you FIRST. If you want FIRST, you'll have to use raw SQL:

DB.run "ALTER TABLE `myTable` ADD COLUMN `id` INT AUTO_INCREMENT UNIQUE FIRST"

The other difference is your SQL doesn't specify NOT NULL, but your Sequel code does.

Not the answer you're looking for? Browse other questions tagged or ask your own question.