Skip to content

Commit e3ace59

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

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

tests/unit/sqlalchemy/test_compiler.py

+38
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,25 @@
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+
)
4363

4464
@pytest.fixture
4565
def dialect():
@@ -170,3 +190,21 @@ def test_try_cast(dialect):
170190
statement = select(try_cast(table_without_catalog.c.id, String))
171191
query = statement.compile(dialect=dialect)
172192
assert str(query) == 'SELECT try_cast("table".id as VARCHAR) AS id \nFROM "table"'
193+
194+
195+
def test_catalogs_create_table_with_pk(dialect):
196+
statement = CreateTable(table_with_pk)
197+
query = statement.compile(dialect=dialect)
198+
assert 'primary key' not in str(query).lower()
199+
200+
201+
def test_catalogs_create_table_with_fk(dialect):
202+
statement = CreateTable(table_with_fk)
203+
query = statement.compile(dialect=dialect)
204+
assert 'foreign key' not in str(query).lower()
205+
206+
207+
def test_catalogs_create_table_with_unique(dialect):
208+
statement = CreateTable(table_with_unique)
209+
query = statement.compile(dialect=dialect)
210+
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)