Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add autoincrement column that is not primary key Alembic

I want to add a column that is autoincrement that is not primary key to an existing MySQL database.

The command issued on the server required for this operation is the following:

ALTER TABLE `mytable` ADD `id` INT UNIQUE NOT NULL AUTO_INCREMENT FIRST

The issue I face is how to replicate this table change through an Alembic migration. I have tried:

from alembic import op
import sqlalchemy as sa

def upgrade():
    op.add_column('mytable', sa.Colummn('id', sa.INTEGER(), 
                  nullable=False, autoincrement=True)

but when I try to insert a row with the following command:

INSERT INTO `mytable` (`col1`, `col2`) VALUES (`bar`);

where col1, col2 are non nullable columns. When I insert this record I expect the table to generate automatically the id for me.

ERROR 1364 (HY000): Field 'id' doesn't have a default value

If I inspect the sql autogenerated by Alembic using the following command:

alembic upgrade 'hash-of-revision' --sql

it spits out, for the given revision:

ALTER TABLE mytable ADD COLUMN id INTEGER NOT NULL;

Which means that either Alembic or SQLAlchemy is ignoring the autoincrement field when generating the sql of the migration.

Is there a way I can solve this? Or can I establish a migration based on a custom sql command?

like image 742
adrpino Avatar asked May 08 '18 11:05

adrpino


People also ask

Can you auto increment a non primary key?

There can be only one AUTO_INCREMENT column per table, it must be indexed, and it cannot have a DEFAULT value. So you can indeed have an AUTO_INCREMENT column in a table that is not the primary key.

How do I add an autoincrement column?

To add a new AUTO_INCREMENT integer column named c : ALTER TABLE t2 ADD c INT UNSIGNED NOT NULL AUTO_INCREMENT, ADD PRIMARY KEY (c); We indexed c (as a PRIMARY KEY ) because AUTO_INCREMENT columns must be indexed, and we declare c as NOT NULL because primary key columns cannot be NULL .

How can I add column to auto increment in SQL Server?

If you're looking to add auto increment to an existing table by changing an existing int column to IDENTITY , SQL Server will fight you. You'll have to either: Add a new column all together with new your auto-incremented primary key, or. Drop your old int column and then add a new IDENTITY right after.


1 Answers

First we add the column and allow it to have null values

op.add_column('abc', Column('id', BIGINT(unsigned=True), comment='This column stores the type of phrase') )

Then we create the primary key

op.create_primary_key( 'abc_pk', table_name='abc', columns=['id'] )

Not sure how it allows me to add a primary key on an empty column but i guess it is because it is in a transaction block.

Then we alter the column to have the autoincrement column

op.alter_column( existing_type=BIGINT(unsigned=True), table_name='abc', column_name='id', autoincrement=True, existing_autoincrement=True, nullable=False)

like image 191
Nelson Cajetin Diego Alfonso Avatar answered Oct 06 '22 00:10

Nelson Cajetin Diego Alfonso