Skip to content

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

Description

@cansjt

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

Error

  File "~/.virtualenvs/project/bin/alembic", line 8, in <module>
    sys.exit(main())
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/config.py", line 581, in main
    CommandLine(prog=prog).main(argv=argv)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/config.py", line 575, in main
    self.run_cmd(cfg, options)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/config.py", line 555, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/command.py", line 298, in upgrade
    script.run_env()
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/script/base.py", line 489, in run_env
    util.load_python_file(self.dir, "env.py")
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/util/compat.py", line 184, in load_module_py
    spec.loader.exec_module(module)
  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/env.py", line 145, in <module>
    run_migrations_online()
  File "src/dags/stats/migrations/env.py", line 138, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
    self.get_context().run_migrations(**kw)
  File "~/.virtualenvs/project/lib/python3.7/site-packages/alembic/runtime/migration.py", line 522, in run_migrations
    step.migration_fn(**kw)
  File "src/dags/stats/migrations/versions/792d2e012c4a_migration_script_.py", 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/ops.py", line 1956, in batch_add_column
    return operations.invoke(op)
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/base.py", line 373, in invoke
    return fn(self, operation)
  File "~/.virtualenvs/jfdags/lib/python3.7/site-packages/alembic/operations/toimpl.py", line 146, in add_column
    operations.impl.create_column_comment(column)
AttributeError: 'BatchOperationsImpl' object has no attribute 'create_column_comment'

Versions.

  • 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 !

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions