From 41656f830c41734063a9460d2800c74123837de6 Mon Sep 17 00:00:00 2001 From: Mike Fiedler Date: Sun, 3 Jul 2022 17:56:07 -0400 Subject: feat: add `drop constraint if exists` to compiler ### Description Add `DROP CONSTRAINT ... IF EXISTS` behavior to the compiler. Fixes https://github.com/sqlalchemy/sqlalchemy/issues/8141 ### Checklist This pull request is: - [ ] A documentation / typographical error fix - Good to go, no issue or tests are needed - [ ] A short code fix - please include the issue number, and create an issue if none exists, which must include a complete example of the issue. one line code fixes without an issue and demonstration will not be accepted. - Please include: `Fixes: #` in the commit message - please include tests. one line code fixes without tests will not be accepted. - [x] A new feature implementation - please include the issue number, and create an issue if none exists, which must include a complete example of how the feature would look. - Please include: `Fixes: #` in the commit message - please include tests. **Have a nice day!** Closes: #8161 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/8161 Pull-request-sha: 43276e29fa864fc66900c5a3fa0bf84df5f14271 Change-Id: I18bae3cf013159b6fffde4413fb59ce19ff83c16 --- lib/sqlalchemy/sql/compiler.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'lib/sqlalchemy/sql/compiler.py') diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index 60ec09771..817fc9315 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -5216,10 +5216,11 @@ class DDLCompiler(Compiled): "Can't emit DROP CONSTRAINT for constraint %r; " "it has no name" % drop.element ) - return "ALTER TABLE %s DROP CONSTRAINT %s%s" % ( + return "ALTER TABLE %s DROP CONSTRAINT %s%s%s" % ( self.preparer.format_table(drop.element.table), + "IF EXISTS " if drop.if_exists else "", formatted_name, - drop.cascade and " CASCADE" or "", + " CASCADE" if drop.cascade else "", ) def get_column_specification(self, column, **kwargs): -- cgit v1.2.1