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

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

Closed
sqlalchemy-bot opened this Issue May 18, 2017 · 5 comments

Comments

Projects
None yet
1 participant
@sqlalchemy-bot

sqlalchemy-bot commented May 18, 2017

Migrated issue, originally created by taffit (@taffit)

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)
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented May 18, 2017

Changes by taffit (@taffit):

  • edited description
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented May 18, 2017

Michael Bayer (@zzzeek) wrote:

it's also not quoting the second term ? that's weird. docs say it should be, I will add that also.

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented May 18, 2017

Michael Bayer (@zzzeek) wrote:

uhhh let me be careful w/ that. there's already some quoting happening here if names are case-sens.

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented May 18, 2017

Michael Bayer (@zzzeek) wrote:

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

c7da7f2

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented May 18, 2017

Changes by Michael Bayer (@zzzeek):

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