Skip to content

Commit 2108c38

Browse files
Ignore and warn for unsupported constraints for DDL generation
Ignore primary key, foreign key and unique constraints for DDL generation and warn when they get ignored. Co-authored-by: Damian Owsianny <[email protected]>
1 parent d6b0920 commit 2108c38

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

tests/unit/sqlalchemy/test_compiler.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@
1111
# limitations under the License.
1212
import pytest
1313
from sqlalchemy import Column
14+
from sqlalchemy import ForeignKey
1415
from sqlalchemy import func
1516
from sqlalchemy import insert
1617
from sqlalchemy import Integer
1718
from sqlalchemy import MetaData
1819
from sqlalchemy import select
1920
from sqlalchemy import String
2021
from sqlalchemy import Table
22+
from sqlalchemy.exc import SAWarning
2123
from sqlalchemy.schema import CreateTable
2224
from sqlalchemy.sql import column
2325
from sqlalchemy.sql import table
@@ -40,6 +42,26 @@
4042
trino_catalog='other'
4143
)
4244

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

4466
@pytest.fixture
4567
def dialect():
@@ -170,3 +192,24 @@ def test_try_cast(dialect):
170192
statement = select(try_cast(table_without_catalog.c.id, String))
171193
query = statement.compile(dialect=dialect)
172194
assert str(query) == 'SELECT try_cast("table".id as VARCHAR) AS id \nFROM "table"'
195+
196+
197+
def test_catalogs_create_table_with_pk(dialect):
198+
with pytest.warns(SAWarning, match="Trino does not support PRIMARY KEY constraints. Constraint will be ignored."):
199+
statement = CreateTable(table_with_pk)
200+
query = statement.compile(dialect=dialect)
201+
assert 'primary key' not in str(query).lower()
202+
203+
204+
def test_catalogs_create_table_with_fk(dialect):
205+
with pytest.warns(SAWarning, match="Trino does not support FOREIGN KEY constraints. Constraint will be ignored."):
206+
statement = CreateTable(table_with_fk)
207+
query = statement.compile(dialect=dialect)
208+
assert 'foreign key' not in str(query).lower()
209+
210+
211+
def test_catalogs_create_table_with_unique(dialect):
212+
with pytest.warns(SAWarning, match="Trino does not support UNIQUE constraints. Constraint will be ignored."):
213+
statement = CreateTable(table_with_unique)
214+
query = statement.compile(dialect=dialect)
215+
assert 'unique' not in str(query).lower()

trino/sqlalchemy/compiler.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from sqlalchemy.sql import sqltypes
1515
from sqlalchemy.sql.base import DialectKWArgs
1616
from sqlalchemy.sql.functions import GenericFunction
17+
from sqlalchemy.util import warn as SAWarn
1718

1819
# https://trino.io/docs/current/language/reserved.html
1920
RESERVED_WORDS = {
@@ -181,7 +182,17 @@ def visit_try_cast(self, element, **kw):
181182

182183

183184
class TrinoDDLCompiler(compiler.DDLCompiler):
184-
pass
185+
def visit_foreign_key_constraint(self, constraint, **kw):
186+
SAWarn("Trino does not support FOREIGN KEY constraints. Constraint will be ignored.")
187+
return None
188+
189+
def visit_primary_key_constraint(self, constraint, **kw):
190+
SAWarn("Trino does not support PRIMARY KEY constraints. Constraint will be ignored.")
191+
return None
192+
193+
def visit_unique_constraint(self, constraint, **kw):
194+
SAWarn("Trino does not support UNIQUE constraints. Constraint will be ignored.")
195+
return None
185196

186197

187198
class TrinoTypeCompiler(compiler.GenericTypeCompiler):

0 commit comments

Comments
 (0)