From a72748b85099af415003810abb8f9e10daae8efa Mon Sep 17 00:00:00 2001 From: Andreas Motl Date: Thu, 28 Sep 2023 15:26:58 +0200 Subject: [PATCH] SQLAlchemy: Improve DDL compiler to ignore unique key constraints --- CHANGES.txt | 3 ++- src/crate/client/sqlalchemy/compiler.py | 7 ++++++ .../client/sqlalchemy/tests/compiler_test.py | 25 +++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) diff --git a/CHANGES.txt b/CHANGES.txt index d09a90ee..4fbb10b9 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -13,7 +13,8 @@ Unreleased certificate details are immanent, like no longer accepting the long deprecated ``commonName`` attribute. Instead, going forward, only the ``subjectAltName`` attribute will be used. -- SQLAlchemy: Improve DDL compiler to ignore foreign key constraints +- SQLAlchemy: Improve DDL compiler to ignore foreign key and uniqueness + constraints .. _urllib3 v2.0 migration guide: https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html .. _urllib3 v2.0 roadmap: https://urllib3.readthedocs.io/en/stable/v2-roadmap.html diff --git a/src/crate/client/sqlalchemy/compiler.py b/src/crate/client/sqlalchemy/compiler.py index 1995e050..4906dcba 100644 --- a/src/crate/client/sqlalchemy/compiler.py +++ b/src/crate/client/sqlalchemy/compiler.py @@ -184,6 +184,13 @@ def visit_foreign_key_constraint(self, constraint, **kw): """ return None + def visit_unique_constraint(self, constraint, **kw): + """ + CrateDB does not support unique key constraints. + """ + return None + + class CrateTypeCompiler(compiler.GenericTypeCompiler): def visit_string(self, type_, **kw): diff --git a/src/crate/client/sqlalchemy/tests/compiler_test.py b/src/crate/client/sqlalchemy/tests/compiler_test.py index fa24e1a6..002ddd88 100644 --- a/src/crate/client/sqlalchemy/tests/compiler_test.py +++ b/src/crate/client/sqlalchemy/tests/compiler_test.py @@ -346,3 +346,28 @@ class ItemStore(Base): ) """)) # noqa: W291, W293 + + def test_ddl_with_unique_key(self): + """ + Verify the CrateDB dialect properly ignores unique key constraints. + """ + + Base = sa.orm.declarative_base(metadata=self.metadata) + + class FooBar(Base): + """The entity.""" + + __tablename__ = "foobar" + + id = sa.Column(sa.Integer, primary_key=True) + name = sa.Column(sa.String, unique=True) + + self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False) + self.assertEqual(self.executed_statement, dedent(""" + CREATE TABLE testdrive.foobar ( + \tid INT NOT NULL, + \tname STRING, + \tPRIMARY KEY (id) + ) + + """)) # noqa: W291