`op.rename_table()` for MSSQL-dialect using schema creates wrong SQL

Issue #429 resolved
taffit
created an issue

If you provide a schema to the op.rename_table(<old_name>, <new_name>, schema=<schema>)-method, it is translated to EXEC sp_rename '<schema>.<old_name>', '<schema>.<new_name>' which however a) breaks the alembic migration and b) would result in a table name <schema>.<schema>.<new_name>.

If you take a look at the documentation of sp_rename here you will read the following:

... new_name must be a one-part name ...

Solution:

In the file alembic/ddl/mssql.py, around line 230, change the following

format_table_name(compiler, element.new_table_name, element.schema)

to the following:

format_table_name(compiler, element.new_table_name, None)

Comments (4)

  1. Michael Bayer repo owner

    Don't use schema with second argument, SQL Server sp_rename

    Repaired :meth:.Operations.rename_table for SQL Server when the target table is in a remote schema, the schema name is omitted from the "new name" argument.

    Also added some extra tests for sp_rename w/ quoting / case sensitive names.

    Change-Id: I411b32d0c5bba5a466c0b5d6a412c1b7541fdc95 Fixes: #429

    → <<cset c7da7f2d08d4>>

  2. Log in to comment