From ba624b34e8cb1e9a1f2adbaa923a7fd5fe5ba25c 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 | 31 ++++++++++++++++++- 3 files changed, 39 insertions(+), 2 deletions(-) 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..f9b4eef8 100644 --- a/src/crate/client/sqlalchemy/tests/compiler_test.py +++ b/src/crate/client/sqlalchemy/tests/compiler_test.py @@ -27,6 +27,10 @@ import sqlalchemy as sa from sqlalchemy.sql import text, Update +try: + from sqlalchemy.orm import declarative_base +except ImportError: + from sqlalchemy.ext.declarative import declarative_base from crate.client.sqlalchemy.sa_version import SA_VERSION, SA_1_4, SA_2_0 from crate.client.sqlalchemy.types import ObjectType @@ -294,7 +298,7 @@ def test_ddl_with_foreign_keys(self): Verify the CrateDB dialect properly ignores foreign key constraints. """ - Base = sa.orm.declarative_base(metadata=self.metadata) + Base = declarative_base(metadata=self.metadata) class RootStore(Base): """The main store.""" @@ -346,3 +350,28 @@ class ItemStore(Base): ) """)) # noqa: W291, W293 + + def test_ddl_with_unique_key(self): + """ + Verify the CrateDB dialect properly ignores unique key constraints. + """ + + Base = 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