Skip to content

Commit 9eaf38d

Browse files
committed
SQLAlchemy: Improve DDL compiler to ignore unique key constraints
1 parent 279434c commit 9eaf38d

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
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: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
import sqlalchemy as sa
2929
from sqlalchemy.sql import text, Update
30+
try:
31+
from sqlalchemy.orm import declarative_base
32+
except ImportError:
33+
from sqlalchemy.ext.declarative import declarative_base
3034

3135
from crate.client.sqlalchemy.sa_version import SA_VERSION, SA_1_4, SA_2_0
3236
from crate.client.sqlalchemy.types import ObjectType
@@ -294,7 +298,7 @@ def test_ddl_with_foreign_keys(self):
294298
Verify the CrateDB dialect properly ignores foreign key constraints.
295299
"""
296300

297-
Base = sa.orm.declarative_base(metadata=self.metadata)
301+
Base = declarative_base(metadata=self.metadata)
298302

299303
class RootStore(Base):
300304
"""The main store."""
@@ -346,3 +350,28 @@ class ItemStore(Base):
346350
)
347351
348352
""")) # noqa: W291, W293
353+
354+
def test_ddl_with_unique_key(self):
355+
"""
356+
Verify the CrateDB dialect properly ignores unique key constraints.
357+
"""
358+
359+
Base = declarative_base(metadata=self.metadata)
360+
361+
class FooBar(Base):
362+
"""The entity."""
363+
364+
__tablename__ = "foobar"
365+
366+
id = sa.Column(sa.Integer, primary_key=True)
367+
name = sa.Column(sa.String, unique=True)
368+
369+
self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False)
370+
self.assertEqual(self.executed_statement, dedent("""
371+
CREATE TABLE testdrive.foobar (
372+
\tid INT NOT NULL,
373+
\tname STRING,
374+
\tPRIMARY KEY (id)
375+
)
376+
377+
""")) # noqa: W291

0 commit comments

Comments
 (0)