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

rewriter is incompatible with multi-db scheme #505

sqlalchemy-bot opened this issue Aug 31, 2018 · 1 comment

rewriter is incompatible with multi-db scheme #505

sqlalchemy-bot opened this issue Aug 31, 2018 · 1 comment


Copy link

@sqlalchemy-bot sqlalchemy-bot commented Aug 31, 2018

Migrated issue, originally created by Michael Bayer (@zzzeek)

def create_table(context, revision, op):
    return op

def run_migrations_online():
    """Run migrations in 'online' mode.

    In this scenario we need to create an Engine
    and associate a connection with the context.

    connectable = engine_from_config(

    with connectable.connect() as connection:

        for schema_name in ['test_schema', 'test_schema_2']:
            conn = connection
            conn.execute("SET search_path TO %s" % schema_name)
                upgrade_token="%s_upgrades" % schema_name,
                downgrade_token="%s_downgrades" % schema_name,

            with context.begin_transaction():

because the upgradeops is visited multiple times, we get:


File "foo/", line 99, in <module>
  File "foo/", line 94, in run_migrations_online
  File "<string>", line 8, in run_migrations
  File "/home/classic/dev/alembic/alembic/runtime/", line 836, in run_migrations
  File "/home/classic/dev/alembic/alembic/runtime/", line 321, in run_migrations
    for step in self._migrations_fn(heads, self):
  File "/home/classic/dev/alembic/alembic/", line 156, in retrieve_migrations
    revision_context.run_autogenerate(rev, context)
  File "/home/classic/dev/alembic/alembic/autogenerate/", line 415, in run_autogenerate
    self._run_environment(rev, migration_context, True)
  File "/home/classic/dev/alembic/alembic/autogenerate/", line 459, in _run_environment
    hook(migration_context, rev, self.generated_revisions)
  File "/home/classic/dev/alembic/alembic/autogenerate/", line 102, in __call__
    self.process_revision_directives(context, revision, directives)
  File "/home/classic/dev/alembic/alembic/autogenerate/", line 150, in process_revision_directives
    self._traverse_list(context, revision, directives)
  File "/home/classic/dev/alembic/alembic/autogenerate/", line 145, in _traverse_list
    dest.extend(self._traverse_for(context, revision, directive))
  File "/home/classic/dev/alembic/alembic/autogenerate/", line 139, in _traverse_for
    traverser(self, context, revision, directive)
  File "/home/classic/dev/alembic/alembic/autogenerate/", line 110, in _traverse_script
    ret = self._traverse_for(context, revision, directive.upgrade_ops)
  File "/home/classic/dev/alembic/alembic/operations/", line 1999, in upgrade_ops
    "This MigrationScript instance has a multiple-entry "
ValueError: This MigrationScript instance has a multiple-entry list for UpgradeOps; please use the upgrade_ops_list attribute.

not sure how we can fix this because the traversal keeps happening repeatedly.

Copy link

@sqla-tester sqla-tester commented Sep 19, 2019

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

Memoize elements in rewriter; use correct iteration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants