3

I have a table where I need to add column say a_1 after column a and the table structure is like column a,b,c,d? How can i add this?

Below query adds column after d but I want to add column after a?

def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('sample_table', sa.Column('a_1', mysql.JSON(), nullable=True, ))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('sample_table', 'a_1')
# ### end Alembic commands ###
1
  • 4
    Class alembic.operations.BatchOperations has a method add_column(column, insert_before=None, insert_after=None). alembic.sqlalchemy.org/en/latest/ops.html Commented Mar 19, 2021 at 7:59

2 Answers 2

0
  1. Based on answer for similar question, alembic batch operation:
from alembic import op
import sqlalchemy as sa


def upgrade():
  with op.batch_alter_table('sample_table') as batch_op:
    batch_op.add_column(
      sa.Column('a_1', ...),
      insert_after='a',
    )


def downgrade():
  op.drop_column('sample_table', 'a_1')

The feature introduced with alembic 1.4.0. Reference in docs.

= = =

  1. The docs describe "recreate" argument.

In my case [postgres 15, alembic 1.7.4] the above doesn't work without explicit recreate="always". It means a new table is created with all the data copied from existing one. The default recreate="auto" may perform the same actions.

def upgrade():
  with op.batch_alter_table('sample_table', recreate='always') as batch_op:
    ...

= = =

  1. Some other ways:
  • sql dialect may allow .. ADD COLUMN .. AFTER <existing column> like MySql

  • recreate table with raw sql

  • db management tools (I'm not familiar with)

Alembic allows raw sql execution with op.execute(): more docs.

Sign up to request clarification or add additional context in comments.

Comments

-5
add_column(table_name, column, schema=None)

Issue an “add column” instruction using the current migration context.

e.g.:

from alembic import op
from sqlalchemy import Column, String

op.add_column('organization',
    Column('name', String())
)

The provided Column object can also specify a ForeignKey, referencing a remote table name. Alembic will automatically generate a stub “referenced” table and emit a second ALTER statement in order to add the constraint separately:

from alembic import op
from sqlalchemy import Column, INTEGER, ForeignKey

op.add_column('organization',
    Column('account_id', INTEGER, ForeignKey('accounts.id'))
)

Note that this statement uses the Column construct as is from the SQLAlchemy library. In particular, default values to be created on the database side are specified using the server_default parameter, and not default which only specifies Python-side defaults:

from alembic import op
from sqlalchemy import Column, TIMESTAMP, func

# specify "DEFAULT NOW" along with the column add
op.add_column('account',
    Column('timestamp', TIMESTAMP, server_default=func.now())
)

Parameters table_name – String name of the parent table.

column – a sqlalchemy.schema.Column object representing the new column.

schema – Optional schema name to operate within. To control quoting of the schema outside of the default behavior, use the SQLAlchemy construct quoted_name.

Source https://alembic.sqlalchemy.org/en/latest/ops.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.