From 82eb9f8a5bbb5e6b929d2f0ae5b934e6a253f94e Mon Sep 17 00:00:00 2001 From: Olga Kleitsa Date: Sat, 12 May 2018 09:29:00 +0300 Subject: [PATCH] included actual sql query to discover fi foreign key with the same name exists in a specific table of the database in use (#1896) --- dialects/mssql/mssql.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dialects/mssql/mssql.go b/dialects/mssql/mssql.go index e0606465..a8d3c45a 100644 --- a/dialects/mssql/mssql.go +++ b/dialects/mssql/mssql.go @@ -130,7 +130,14 @@ func (s mssql) RemoveIndex(tableName string, indexName string) error { } func (s mssql) HasForeignKey(tableName string, foreignKeyName string) bool { - return false + var count int + currentDatabase, tableName := currentDatabaseAndTable(&s, tableName) + s.db.QueryRow(`SELECT count(*) + FROM sys.foreign_keys as F inner join sys.tables as T on F.parent_object_id=T.object_id + inner join information_schema.tables as I on I.TABLE_NAME = T.name + WHERE F.name = ? + AND T.Name = ? AND I.TABLE_CATALOG = ?;`, foreignKeyName, tableName, currentDatabase).Scan(&count) + return count > 0 } func (s mssql) HasTable(tableName string) bool {