Foreign Key Constraints after Table Rename
Asked Answered
K

1

5

I'm using FluentMigrator for database migrations on a SQL Server 2014 database. If I use the Rename builder to rename a table as such:

Rename.Table("Old Name").To("NewName");

Do I then need to drop and remake any foreign keys that reference the table's old name or does SQL (or FM) know to update the references?

Kassandra answered 19/12, 2016 at 16:27 Comment(0)
C
7

You don't have to worry about foreign keys it will be automatically referring to new table name.

But if the table is used in any stored procedure, function, view or trigger then after renaming the table you have to manually change the scripts to refer the new name else while executing any of the above mentioned objects you will get invalid object name error

As mentioned in comments if the old table name is used in application codes then again you need to manually change it to new table name

Cartulary answered 19/12, 2016 at 16:36 Comment(3)
And if it is used in any code like in a c# application you also have to edit that.Irairacund
Perfect. Thanks for confirming. I'm surprised it was so difficult to find a solid answer to this question.Kassandra
"You don't have to worry about foreign keys it will be automatically referring to new table name." Is this because of using FluentMigrator? @CartularyNeolith

© 2022 - 2024 — McMap. All rights reserved.