From 1da7aed8c9b482c83466eac32369295dac1aa225 Mon Sep 17 00:00:00 2001 From: Piotr Czeszkiewicz <41184309+pczeszkiewicz@users.noreply.github.com> Date: Sat, 20 Apr 2024 10:39:10 +0200 Subject: [PATCH] Fixed dropping foreign keys from tables on schemas Currently it doesn't work if a table exists in non default `dbo` schema and migrations raises exception. This fix solves problem by giving schema name to `ALTER` clause. --- mssql/schema.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mssql/schema.py b/mssql/schema.py index 73726fcc..f48e656e 100644 --- a/mssql/schema.py +++ b/mssql/schema.py @@ -82,11 +82,11 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): FROM sys.foreign_keys WHERE referenced_object_id = object_id('%(table)s') SELECT - @sql_drop_constraint = 'ALTER TABLE [' + OBJECT_NAME(parent_object_id) + '] ' + + @sql_drop_constraint = 'ALTER TABLE [' + sch.name + '].[' + OBJECT_NAME(parent_object_id) + '] ' + 'DROP CONSTRAINT [' + @sql_foreign_constraint_name + '] ' - FROM sys.foreign_keys - WHERE referenced_object_id = object_id('%(table)s') and name = @sql_foreign_constraint_name - exec sp_executesql @sql_drop_constraint + FROM sys.foreign_keys fk + INNER JOIN sys.schemas sch ON fk.schema_id = sch.schema_id + WHERE fk.referenced_object_id = object_id('%(table)s') and fk.name = @sql_foreign_constraint_name END DROP TABLE %(table)s """