diff options
author | Mike Fiedler <miketheman@gmail.com> | 2022-07-03 17:56:07 -0400 |
---|---|---|
committer | sqla-tester <sqla-tester@sqlalchemy.org> | 2022-07-03 17:56:07 -0400 |
commit | 41656f830c41734063a9460d2800c74123837de6 (patch) | |
tree | 656364c281adcc9a3ac9907bc1cec99c9d1f09e2 /lib/sqlalchemy/sql/compiler.py | |
parent | 4b3f204d07d53ae09b59ce8f33b534f26a605cd4 (diff) | |
download | sqlalchemy-41656f830c41734063a9460d2800c74123837de6.tar.gz |
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
<!-- go over following points. check them with an `x` if they do apply, (they turn into clickable checkboxes once the PR is submitted, so no need to do everything at once)
-->
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: #<issue number>` 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: #<issue number>` 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
Diffstat (limited to 'lib/sqlalchemy/sql/compiler.py')
-rw-r--r-- | lib/sqlalchemy/sql/compiler.py | 5 |
1 files changed, 3 insertions, 2 deletions
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): |