Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FluentMigrator - Check if Foreign Key exists before deleting it

I am using FluentMigrator to migrate one database schema to another. I have a case in which I want to check if a foreign key exists before deleting it.

Previously, I just delete the foreign key by doing:

Delete.ForeignKey("FK_TableName_FieldName").OnTable("TableName");

How do I check that the foreign key exists first?

like image 980
noblerare Avatar asked Sep 19 '18 14:09

noblerare


1 Answers

This is how to delete a foreign key if it exists using FluentMigrator:

if (Schema.Table("TableName").Constraint("FK_TableName_FieldName").Exists())
{
   Delete.ForeignKey("FK_TableName_FieldName").OnTable("TableName");
}
like image 142
Martin D. Avatar answered Oct 30 '22 22:10

Martin D.