From f7d4d7344e7860f06cecb7f36ed7e550cea4cab7 Mon Sep 17 00:00:00 2001 From: Will Morrison Date: Tue, 18 Mar 2025 10:45:43 -0400 Subject: [PATCH] Ignore primary key, foreign key and unique constraints for DDL generation --- tests/unit/sqlalchemy/test_compiler.py | 39 ++++++++++++++++++++++++++ trino/sqlalchemy/compiler.py | 9 +++++- 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/tests/unit/sqlalchemy/test_compiler.py b/tests/unit/sqlalchemy/test_compiler.py index 4da5232e..65da081d 100644 --- a/tests/unit/sqlalchemy/test_compiler.py +++ b/tests/unit/sqlalchemy/test_compiler.py @@ -11,6 +11,7 @@ # limitations under the License. import pytest from sqlalchemy import Column +from sqlalchemy import ForeignKey from sqlalchemy import func from sqlalchemy import insert from sqlalchemy import Integer @@ -40,6 +41,26 @@ trino_catalog='other' ) +table_with_pk = Table( + 'table_with_pk', + metadata, + Column('id', String, primary_key=True) +) + +table_with_fk = Table( + 'table_with_fk', + metadata, + Column('id', String, primary_key=True), + Column('fk', String, ForeignKey('table_with_pk.id')) +) + +table_with_unique = Table( + 'table_with_constraint', + metadata, + Column('id', String, primary_key=True), + Column('uniq', String, unique=True) +) + @pytest.fixture def dialect(): @@ -170,3 +191,21 @@ def test_try_cast(dialect): statement = select(try_cast(table_without_catalog.c.id, String)) query = statement.compile(dialect=dialect) assert str(query) == 'SELECT try_cast("table".id as VARCHAR) AS id \nFROM "table"' + + +def test_catalogs_create_table_with_pk(dialect): + statement = CreateTable(table_with_pk) + query = statement.compile(dialect=dialect) + assert 'primary key' not in str(query).lower() + + +def test_catalogs_create_table_with_fk(dialect): + statement = CreateTable(table_with_fk) + query = statement.compile(dialect=dialect) + assert 'foreign key' not in str(query).lower() + + +def test_catalogs_create_table_with_unique(dialect): + statement = CreateTable(table_with_unique) + query = statement.compile(dialect=dialect) + assert 'unique' not in str(query).lower() diff --git a/trino/sqlalchemy/compiler.py b/trino/sqlalchemy/compiler.py index 19737761..f988b442 100644 --- a/trino/sqlalchemy/compiler.py +++ b/trino/sqlalchemy/compiler.py @@ -181,7 +181,14 @@ def visit_try_cast(self, element, **kw): class TrinoDDLCompiler(compiler.DDLCompiler): - pass + def visit_foreign_key_constraint(self, constraint, **kw): + return None + + def visit_primary_key_constraint(self, constraint, **kw): + return None + + def visit_unique_constraint(self, constraint, **kw): + return None class TrinoTypeCompiler(compiler.GenericTypeCompiler):