Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One cannot add a column with a comment within batch operation context #761

cansjt opened this issue Nov 27, 2020 · 3 comments
batch migrations bug Something isn't working


Copy link

cansjt commented Nov 27, 2020

Describe the bug
Batch operation op.batch_alter_table(...).add_column() method cannot add a column that comes with a comment.

Comment support is fairly new, maybe this is a "feature" due to some constraint I am not aware of ?

Expected behavior

The column is added to the table and the appropriate comment is defined for the column.

To Reproduce

I believe the following migration script, the one I found the issue with, stripped of anything unrelated to the bug, should provide the minimal amount of code to reproduce the bug:

from alembic import op
import sqlalchemy as sa

revision = '792d2e012c4a'
down_revision = None  # Obviously there exists other migrations that created the table in the first place

def upgrade():
    with op.batch_alter_table('some_table', schema='some_schema') as batch_op:
        batch_op.add_column(sa.Column('column_name', sa.Integer, nullable=True, comment="some comment"))
        # More operations may come here


  File "~/.virtualenvs/project/bin/alembic", line 8, in <module>
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/", line 581, in main
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/", line 575, in main
    self.run_cmd(cfg, options)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/", line 555, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/", line 298, in upgrade
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/script/", line 489, in run_env
    util.load_python_file(self.dir, "")
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/util/", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/util/", line 184, in load_module_py
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "src/dags/stats/migrations/", line 145, in <module>
  File "src/dags/stats/migrations/", line 138, in run_migrations_online
  File "<string>", line 8, in run_migrations
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/runtime/", line 846, in run_migrations
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/runtime/", line 522, in run_migrations
  File "src/dags/stats/migrations/versions/", line 51, in upgrade
    batch_op.add_column(sa.Column('column_name', sa.Integer, nullable=True, comment="some comment"))
  File "<string>", line 3, in add_column
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/", line 1956, in batch_add_column
    return operations.invoke(op)
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/", line 373, in invoke
    return fn(self, operation)
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/", line 146, in add_column
AttributeError: 'BatchOperationsImpl' object has no attribute 'create_column_comment'


  • OS: Ubuntu 2020.04
  • Python: 3.7.9
  • Alembic: 1.4.3
  • SQLAlchemy: 1.3.20
  • Database: Postgresql
  • DBAPI: psycopg2

Additional context

Have a nice day!
You too !

@cansjt cansjt added the requires triage New issue that requires categorization label Nov 27, 2020
@zzzeek zzzeek added batch migrations bug Something isn't working and removed requires triage New issue that requires categorization labels Nov 27, 2020
Copy link

Mike Bayer has proposed a fix for this issue in the master branch:

Implement column comments for batch

Copy link

zzzeek commented Nov 27, 2020

we're on a slightly delayed path for relase as it's 1.5, feel free to test alembic master to see if that works on your end thanks!

Copy link
Contributor Author

cansjt commented Dec 18, 2020

Thank you for the blazing fast fix. 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
batch migrations bug Something isn't working
None yet

No branches or pull requests

3 participants