sqlalchemy / sqlalchemy/alembic

'BatchOperations' object has no attribute 'rename_table'

Open
#1,453 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

batch migrations bug documentation
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

Describe the bug

After an upgrade to 1.11.x, I now see the following error messages in a migration:

AttributeError: 'BatchOperations' object has no attribute 'rename_table'

The migration in question is doing this:

def upgrade_export_locations_table(connection):
    ...

    with op.batch_alter_table("share_export_locations") as batch_op:
        batch_op.drop_constraint('sel_id_fk', type_='foreignkey')
        batch_op.drop_column('share_id')
        batch_op.rename_table('share_export_locations',
                              'share_instance_export_locations')

Things worked (or at least, didn't fail) on 1.10.4 and before. Now, the ops docs suggest this was never supported, but it worked and therefore we used it.

The fix is rather easy: we can just change the above to:

def upgrade_export_locations_table(connection):
    ...

    with op.batch_alter_table("share_export_locations") as batch_op:
        batch_op.drop_constraint('sel_id_fk', type_='foreignkey')
        batch_op.drop_column('share_id')
    op.rename_table('share_export_locations',
                    'share_instance_export_locations')

...but there's no mention of this change in behavior in the release notes for 1.11. It would be good to add it, even if retrospectively, if this was indeed intentional.

Expected behavior

I'd expect one of the two things to happen:

  • BatchOperations.rename_table is defined as an alias of Operations.rename_table and does the exact same thing
  • There is a note in the release notes for 1.11.0 indicating that this behaviour changed.

(PS: I did try looking at the rel_1_10_4...rel_1_11_0 diff and commit 2aba0ada168d0047d54c7a08b0ffdde3102b716b looks like the most likely candidate for this issue, but I have yet to go much deeper than this)

To Reproduce

See https://github.com/stephenfin/alembic-issue-1453 for a minimal'ish reproducer.

Error

AttributeError: 'BatchOperations' object has no attribute 'rename_table'

Versions.

  • OS: Fedora
  • Python: 3.12
  • Alembic: 1.11.0
  • SQLAlchemy: 1.4.51
  • Database: MySQL
  • DBAPI: mysql

Additional context

Have a nice day!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the BatchOperations.rename_table and Operations.rename_table entry points, the referenced 1.10.4–1.11.0 diff, and commit 2aba0ada168d0047d54c7a08b0ffdde3102b716b. Run the linked minimal reproducer, then compare the operations docs and the 1.11.0 changelog to establish the intended behavior and document or correct it accordingly.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, python
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.