Skip to content

Commit a72748b

Browse files
committed
SQLAlchemy: Improve DDL compiler to ignore unique key constraints
1 parent 2f8e185 commit a72748b

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGES.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ Unreleased
1313
certificate details are immanent, like no longer accepting the long
1414
deprecated ``commonName`` attribute. Instead, going forward, only the
1515
``subjectAltName`` attribute will be used.
16-
- SQLAlchemy: Improve DDL compiler to ignore foreign key constraints
16+
- SQLAlchemy: Improve DDL compiler to ignore foreign key and uniqueness
17+
constraints
1718

1819
.. _urllib3 v2.0 migration guide: https://urllib3.readthedocs.io/en/latest/v2-migration-guide.html
1920
.. _urllib3 v2.0 roadmap: https://urllib3.readthedocs.io/en/stable/v2-roadmap.html

src/crate/client/sqlalchemy/compiler.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,13 @@ def visit_foreign_key_constraint(self, constraint, **kw):
184184
"""
185185
return None
186186

187+
def visit_unique_constraint(self, constraint, **kw):
188+
"""
189+
CrateDB does not support unique key constraints.
190+
"""
191+
return None
192+
193+
187194
class CrateTypeCompiler(compiler.GenericTypeCompiler):
188195

189196
def visit_string(self, type_, **kw):

src/crate/client/sqlalchemy/tests/compiler_test.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,28 @@ class ItemStore(Base):
346346
)
347347
348348
""")) # noqa: W291, W293
349+
350+
def test_ddl_with_unique_key(self):
351+
"""
352+
Verify the CrateDB dialect properly ignores unique key constraints.
353+
"""
354+
355+
Base = sa.orm.declarative_base(metadata=self.metadata)
356+
357+
class FooBar(Base):
358+
"""The entity."""
359+
360+
__tablename__ = "foobar"
361+
362+
id = sa.Column(sa.Integer, primary_key=True)
363+
name = sa.Column(sa.String, unique=True)
364+
365+
self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False)
366+
self.assertEqual(self.executed_statement, dedent("""
367+
CREATE TABLE testdrive.foobar (
368+
\tid INT NOT NULL,
369+
\tname STRING,
370+
\tPRIMARY KEY (id)
371+
)
372+
373+
""")) # noqa: W291

0 commit comments

Comments
 (0)