included actual sql query to discover fi foreign key with the same name exists in a specific table of the database in use (#1896)

This commit is contained in:
Olga Kleitsa 2018-05-12 09:29:00 +03:00 committed by Jinzhu
parent a58b98acee
commit 82eb9f8a5b
1 changed files with 8 additions and 1 deletions

View File

@ -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 {