Skip to content

Commit

Permalink
SQLAlchemy: Improve DDL compiler to ignore unique key constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Sep 28, 2023
1 parent 2f8e185 commit ba624b3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/crate/client/sqlalchemy/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
31 changes: 30 additions & 1 deletion src/crate/client/sqlalchemy/tests/compiler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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

0 comments on commit ba624b3

Please sign in to comment.