Skip to content

Commit 482ccbc

Browse files
committed
Compiler: Make CREATE INDEX a no-op
1 parent 9fc7faa commit 482ccbc

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
# Changelog
2+
- Compiler: Made `CREATE INDEX` a no-op, only emitting `SELECT 1`, because CrateDB
3+
does not support that statement
24

35
## 2026/05/28 0.42.0
46
- Added support for SQL Alchemy 2.1

src/sqlalchemy_cratedb/compiler.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ def visit_unique_constraint(self, constraint, **kw):
200200
)
201201
return
202202

203+
def visit_create_index(self, create, **kw) -> str:
204+
"""
205+
CrateDB does not support `CREATE INDEX` statements.
206+
"""
207+
warnings.warn(
208+
"CrateDB does not support `CREATE INDEX` statements, "
209+
"they will be omitted when generating DDL statements.",
210+
stacklevel=2,
211+
)
212+
return "SELECT 1"
213+
203214

204215
class CrateTypeCompiler(compiler.GenericTypeCompiler):
205216
def visit_string(self, type_, **kw):

tests/compiler_test.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,3 +511,35 @@ class FooBar(Base):
511511
512512
"""),
513513
) # noqa: W291, W293
514+
515+
def test_ddl_with_create_index(self):
516+
"""
517+
Verify the CrateDB dialect properly ignores `CREATE INDEX` statements.
518+
"""
519+
520+
Base = declarative_base(metadata=self.metadata)
521+
522+
class FooBar(Base):
523+
"""The entity."""
524+
525+
__tablename__ = "foobar"
526+
527+
id = sa.Column(sa.Integer, primary_key=True)
528+
name = sa.Column(sa.String, index=True)
529+
530+
with warnings.catch_warnings(record=True) as w:
531+
# Cause all warnings to always be triggered.
532+
warnings.simplefilter("always")
533+
534+
# Verify SQL DDL statement.
535+
self.metadata.create_all(self.engine, tables=[FooBar.__table__], checkfirst=False)
536+
self.assertEqual(self.executed_statement, "SELECT 1")
537+
538+
# Verify if corresponding warning is emitted.
539+
self.assertEqual(len(w), 1)
540+
self.assertIsSubclass(w[-1].category, UserWarning)
541+
self.assertIn(
542+
"CrateDB does not support `CREATE INDEX` statements, "
543+
"they will be omitted when generating DDL statements.",
544+
str(w[-1].message),
545+
)

0 commit comments

Comments
 (0)