Skip to content

Commit 66c389f

Browse files
committed
Ignore primary key, foreign key and unique constraints for DDL generation
1 parent 126d041 commit 66c389f

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

tests/unit/sqlalchemy/test_compiler.py

+39
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from sqlalchemy import select
1919
from sqlalchemy import String
2020
from sqlalchemy import Table
21+
from sqlalchemy import ForeignKey
2122
from sqlalchemy.schema import CreateTable
2223
from sqlalchemy.sql import column
2324
from sqlalchemy.sql import table
@@ -40,6 +41,26 @@
4041
trino_catalog='other'
4142
)
4243

44+
table_with_pk = Table(
45+
'table_with_pk',
46+
metadata,
47+
Column('id', String, primary_key=True)
48+
)
49+
50+
table_with_fk = Table(
51+
'table_with_fk',
52+
metadata,
53+
Column('id', String, primary_key=True),
54+
Column('fk', String, ForeignKey('table_with_pk.id'))
55+
)
56+
57+
table_with_unique = Table(
58+
'table_with_constraint',
59+
metadata,
60+
Column('id', String, primary_key=True),
61+
Column('uniq', String, unique=True)
62+
)
63+
4364

4465
@pytest.fixture
4566
def dialect():
@@ -170,3 +191,21 @@ def test_try_cast(dialect):
170191
statement = select(try_cast(table_without_catalog.c.id, String))
171192
query = statement.compile(dialect=dialect)
172193
assert str(query) == 'SELECT try_cast("table".id as VARCHAR) AS id \nFROM "table"'
194+
195+
196+
def test_catalogs_create_table_with_pk(dialect):
197+
statement = CreateTable(table_with_pk)
198+
query = statement.compile(dialect=dialect)
199+
assert 'primary key' not in str(query).lower()
200+
201+
202+
def test_catalogs_create_table_with_fk(dialect):
203+
statement = CreateTable(table_with_fk)
204+
query = statement.compile(dialect=dialect)
205+
assert 'foreign key' not in str(query).lower()
206+
207+
208+
def test_catalogs_create_table_with_unique(dialect):
209+
statement = CreateTable(table_with_unique)
210+
query = statement.compile(dialect=dialect)
211+
assert 'unique' not in str(query).lower()

trino/sqlalchemy/compiler.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,14 @@ def visit_try_cast(self, element, **kw):
181181

182182

183183
class TrinoDDLCompiler(compiler.DDLCompiler):
184-
pass
184+
def visit_foreign_key_constraint(self, constraint, **kw):
185+
return None
186+
187+
def visit_primary_key_constraint(self, constraint, **kw):
188+
return None
189+
190+
def visit_unique_constraint(self, constraint, **kw):
191+
return None
185192

186193

187194
class TrinoTypeCompiler(compiler.GenericTypeCompiler):

0 commit comments

Comments
 (0)