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

SQL Server Migrations: RenameIndex generates bad SQL when table unspecified #5520

Closed
Morcatko opened this issue May 25, 2016 · 1 comment
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@Morcatko
Copy link

MigrationBuilder.RenameIndex(...) has an optional parameter "table". When this parameter is not provided rename/migration fails

Steps to reproduce

add this to any migration
migrationBuilder.RenameIndex("existingIndex", "newIndexName");

The issue

The migration is executed and fails with
Exception message: System.Data.SqlClient.SqlException (0x80131904): Either the parameter @objname is ambiguous or the claimed @objtype (INDEX) is wrong.

Stack:

   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean openConnection, Boolean closeConnection)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues,Boolean manageConnection)
   at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommandExtensions.ExecuteNonQuery(IEnumerable`1 commands, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Execute(IEnumerable`1 relationalCommands)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Tools.Cli.DatabaseUpdateCommand.<>c__DisplayClass0_0.<Configure>b__0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Cli.Program.Main(String[] args)

Further technical details

The generated SQL looks like this:
EXEC sp_rename N'.existingIndex', N'newIndexName', N'INDEX'; - note the dot in first parameter

Rename SQL is generated here: https://github.com/aspnet/EntityFramework/blob/d5f39e3532c352c227a80ca40237a9121f17dc6e/src/Microsoft.EntityFrameworkCore.SqlServer/Migrations/SqlServerMigrationsSqlGenerator.cs#L113

RenameIndex method should have non-optional table parameter or there should be a check for non-empty string in the linked method to provide better logging.

EF Core version: 1.0.0-rc2-final
EF Core tools version: 1.0.0-preview1-final
Operating system: Win X
DB: SQL 12.0.2000
Visual Studio version: dotnet-cli

@bricelam
Copy link
Contributor

bricelam commented May 26, 2016

We should check for nulls and throw when generating the SQL on SQL Server. The parameter is optional in the API because some databases (like PostgreSQL) only require the index name and schema.

@rowanmiller rowanmiller added this to the 1.0.1 milestone May 26, 2016
@bricelam bricelam self-assigned this May 27, 2016
@rowanmiller rowanmiller removed the pri0 label Jul 6, 2016
@bricelam bricelam changed the title SQL RenameIndex fails when table name is not provided SQL Server Migrations: RenameIndex generates bad SQL when table unspecified Aug 10, 2016
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 10, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants