From 0e7848efae18878e60801fbf0f6ab5e1177dfdb2 Mon Sep 17 00:00:00 2001 From: Claire-ovo <132752951+Claire-ovo@users.noreply.github.com> Date: Fri, 23 Oct 2026 18:43:58 +0800 Subject: [PATCH 01/11] Fix rendering and imports of nested type arguments --- CHANGES.rst | 3 ++ src/sqlacodegen/generators.py | 48 ++++++++++++------- tests/test_nested_types.py | 89 +++++++++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 17 deletions(-) create mode 100644 tests/test_nested_types.py diff --git a/CHANGES.rst b/CHANGES.rst index 40e0b3cf..e6ac58d0 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,6 +3,9 @@ Version history **UNRELEASED** +- Fixed rendering and imports of nested type classes and instances, including + ClickHouse array element types + (`#469 `_; PR by @Claire-ovo) - **BACKWARD INCOMPATIBLE** The SQLModel generator no longer emits ``t_`` ``Table`` variables for many-to-many association tables that have a primary key, and their relationships no longer pass ``secondary`` via ``sa_relationship_kwargs``. diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index a9571572..effc522a 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -571,12 +571,12 @@ def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> s return render_callable("mapped_column", *args, kwargs=kwargs) def _render_column_type_value(self, value: Any) -> str: - if isinstance(value, (JSONB, JSON)): - # Remove astext_type if it's the default - if isinstance(value.astext_type, Text) and value.astext_type.length is None: - value.astext_type = None # type: ignore[assignment] - else: - self.add_import(Text) + if isinstance(value, type) and issubclass(value, TypeEngine): + self.add_import(value) + return value.__name__ + elif isinstance(value, TypeEngine): + self.add_import(value) + return self._render_type(value, nested=True) if isinstance(value, TextClause): self.add_literal_import("sqlalchemy", "text") @@ -659,7 +659,6 @@ def render_column_type(self, column: Column[Any]) -> str: return f"Enum({enum_class_name}, values_callable=lambda cls: [member.value for member in cls]{extra_kwargs})" - args = [] kwargs: dict[str, Any] = {} # Check if this is an ARRAY column with an Enum item type mapped to a Python enum class @@ -682,6 +681,13 @@ def render_column_type(self, column: Column[Any]) -> str: return render_callable("ARRAY", rendered_enum, kwargs=kwargs) + return self._render_type(column_type) + + def _render_type( + self, column_type: TypeEngine[Any], *, nested: bool = False + ) -> str: + args = [] + kwargs: dict[str, Any] = {} sig = inspect.signature(column_type.__class__.__init__) defaults = {param.name: param.default for param in sig.parameters.values()} missing = object() @@ -703,6 +709,19 @@ def render_column_type(self, column: Column[Any]) -> str: use_kwargs = True continue + if ( + param.name == "astext_type" + and isinstance(column_type, (JSONB, JSON)) + and isinstance(value, Text) + and value.length is None + ): + use_kwargs = True + continue + + if nested and param.default is not Parameter.empty: + # Preserve the keyword style previously supplied by type reprs. + use_kwargs = True + rendered_value = self._render_column_type_value(value) if use_kwargs: kwargs[param.name] = rendered_value @@ -724,7 +743,10 @@ def render_column_type(self, column: Column[Any]) -> str: None, ) if vararg and hasattr(column_type, vararg): - varargs_repr = [repr(arg) for arg in getattr(column_type, vararg)] + varargs_repr = [ + self._render_column_type_value(arg) + for arg in getattr(column_type, vararg) + ] args.extend(varargs_repr) # These arguments cannot be autodetected from the Enum initializer @@ -733,15 +755,7 @@ def render_column_type(self, column: Column[Any]) -> str: if (value := getattr(column_type, colname)) is not None: kwargs[colname] = repr(value) - if isinstance(column_type, (JSONB, JSON)): - # Remove astext_type if it's the default - if ( - isinstance(column_type.astext_type, Text) - and column_type.astext_type.length is None - ): - del kwargs["astext_type"] - - if args or kwargs: + if args or kwargs or nested: return render_callable(column_type.__class__.__name__, *args, kwargs=kwargs) else: return column_type.__class__.__name__ diff --git a/tests/test_nested_types.py b/tests/test_nested_types.py new file mode 100644 index 00000000..5de70227 --- /dev/null +++ b/tests/test_nested_types.py @@ -0,0 +1,89 @@ +from __future__ import annotations + +from typing import Any + +import pytest +from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine +from sqlalchemy.orm import clear_mappers, configure_mappers +from sqlalchemy.types import TypeEngine, UserDefinedType + +from sqlacodegen.generators import ( + DataclassGenerator, + DeclarativeGenerator, + SQLModelGenerator, + TablesGenerator, +) + + +class WrappedType(UserDefinedType[list[Any]]): + """Like ClickHouse Array, retain either a type class or an instance.""" + + def __init__(self, item_type: TypeEngine[Any] | type[TypeEngine[Any]]) -> None: + self.item_type = item_type + + @property + def python_type(self) -> type[list[Any]]: + return list + + def get_col_spec(self, **kwargs: Any) -> str: + return "WRAPPED" + + +class TupleType(UserDefinedType[tuple[Any, ...]]): + def __init__(self, *types: TypeEngine[Any] | type[TypeEngine[Any]]) -> None: + self.types = types + + @property + def python_type(self) -> type[tuple[Any, ...]]: + return tuple + + def get_col_spec(self, **kwargs: Any) -> str: + return f"TUPLE({', '.join(str(type_) for type_ in self.types)})" + + +@pytest.mark.parametrize( + "generator_class", + [TablesGenerator, DeclarativeGenerator, DataclassGenerator, SQLModelGenerator], +) +@pytest.mark.parametrize("options", [[], ["keep_dialect_types"]]) +def test_nested_type_arguments( + generator_class: type[TablesGenerator], options: list[str] +) -> None: + metadata = MetaData() + Table( + "nested_types", + metadata, + Column("id", Integer, primary_key=True), + Column("type_class", WrappedType(String), nullable=False), + Column("instance", WrappedType(String()), nullable=False), + Column("parameterized", WrappedType(String(24)), nullable=False), + Column("nested", WrappedType(WrappedType(String)), nullable=False), + Column("variadic", TupleType(Integer, String(12)), nullable=False), + ) + engine = create_engine("sqlite://") + namespace: dict[str, Any] = {} + table = None + try: + code = generator_class(metadata, engine, options).generate() + exec(compile(code, "", "exec", dont_inherit=True), namespace) + if generator_class is TablesGenerator: + table = namespace["t_nested_types"] + else: + table = namespace["NestedTypes"].__table__ + + configure_mappers() + + assert table.c.type_class.type.item_type is String + assert isinstance(table.c.instance.type.item_type, String) + assert table.c.instance.type.item_type.length is None + assert table.c.parameterized.type.item_type.length == 24 + assert isinstance(table.c.nested.type.item_type, WrappedType) + assert table.c.nested.type.item_type.item_type is String + assert table.c.variadic.type.types[0] is Integer + assert isinstance(table.c.variadic.type.types[1], String) + assert table.c.variadic.type.types[1].length == 12 + finally: + clear_mappers() + if generator_class is SQLModelGenerator and table is not None: + table.metadata.remove(table) + engine.dispose() From b1ba7cda954c0484e19b085284905b2293da267c Mon Sep 17 00:00:00 2001 From: Claire-ovo <132752951+Claire-ovo@users.noreply.github.com> Date: Fri, 23 Oct 2026 19:38:46 +0800 Subject: [PATCH 02/11] Preserve named type metadata and test SQLAlchemy 2.1 output --- CHANGES.rst | 2 + src/sqlacodegen/generators.py | 3 + tests/conftest.py | 3 + tests/test_domain_metadata.py | 92 +++++++++++++++++++++++++++++ tests/test_generator_declarative.py | 22 ++++--- tests/test_generator_tables.py | 12 ++-- 6 files changed, 121 insertions(+), 13 deletions(-) create mode 100644 tests/test_domain_metadata.py diff --git a/CHANGES.rst b/CHANGES.rst index e6ac58d0..8a2dbcc4 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -6,6 +6,8 @@ Version history - Fixed rendering and imports of nested type classes and instances, including ClickHouse array element types (`#469 `_; PR by @Claire-ovo) +- Fixed named types referencing a separate metadata instance in generated code, + including PostgreSQL domains attached automatically by SQLAlchemy 2.1 - **BACKWARD INCOMPATIBLE** The SQLModel generator no longer emits ``t_`` ``Table`` variables for many-to-many association tables that have a primary key, and their relationships no longer pass ``secondary`` via ``sa_relationship_kwargs``. diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index effc522a..aa9a6fb9 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -577,6 +577,9 @@ def _render_column_type_value(self, value: Any) -> str: elif isinstance(value, TypeEngine): self.add_import(value) return self._render_type(value, nested=True) + elif isinstance(value, MetaData): + # Named types must share the generated tables' DDL lifecycle. + return self.base.metadata_ref if isinstance(value, TextClause): self.add_literal_import("sqlalchemy", "text") diff --git a/tests/conftest.py b/tests/conftest.py index 59ac9ee9..75626330 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,11 +1,14 @@ from textwrap import dedent import pytest +import sqlalchemy from pytest import FixtureRequest from sqlalchemy.engine import Engine, create_engine from sqlalchemy.orm import clear_mappers, configure_mappers from sqlalchemy.schema import MetaData +SQLALCHEMY_21 = tuple(map(int, sqlalchemy.__version__.split(".")[:2])) >= (2, 1) + @pytest.fixture def engine(request: FixtureRequest) -> Engine: diff --git a/tests/test_domain_metadata.py b/tests/test_domain_metadata.py new file mode 100644 index 00000000..c4262b68 --- /dev/null +++ b/tests/test_domain_metadata.py @@ -0,0 +1,92 @@ +from __future__ import annotations + +from typing import Any + +import pytest +from sqlalchemy import ( + Column, + Integer, + MetaData, + Table, + create_engine, + create_mock_engine, +) +from sqlalchemy.dialects.postgresql import DOMAIN +from sqlalchemy.orm import clear_mappers, configure_mappers +from sqlmodel import SQLModel + +from sqlacodegen.generators import ( + DataclassGenerator, + DeclarativeGenerator, + SQLModelGenerator, + TablesGenerator, +) + + +@pytest.mark.parametrize( + "generator_class", + [TablesGenerator, DeclarativeGenerator, DataclassGenerator, SQLModelGenerator], +) +@pytest.mark.parametrize("options", [[], ["keep_dialect_types"]]) +@pytest.mark.parametrize("schema", [None, "domain_types"]) +def test_domain_metadata( + generator_class: type[TablesGenerator], + options: list[str], + schema: str | None, + monkeypatch: pytest.MonkeyPatch, +) -> None: + # Isolate named-type DDL listeners as well as tables between SQLModel cases. + if generator_class is SQLModelGenerator: + monkeypatch.setattr(SQLModel, "metadata", MetaData()) + metadata = MetaData() + domain = DOMAIN( + "positive_number", Integer, metadata=metadata, schema=schema, check="VALUE > 0" + ) + for name in ("first_item", "second_item"): + Table( + name, + metadata, + Column("id", Integer, primary_key=True), + Column("value", domain, nullable=False), + schema="items", + ) + + statements: list[str] = [] + + def execute(statement: Any, *args: Any, **kwargs: Any) -> None: + statements.append(str(statement.compile(dialect=engine.dialect)).strip()) + + engine = create_mock_engine("postgresql://", execute) + bind = create_engine("postgresql+psycopg://") + namespace: dict[str, Any] = {} + generated_metadata = None + try: + code = generator_class(metadata, bind, options).generate() + exec(compile(code, "", "exec", dont_inherit=True), namespace) + if generator_class is TablesGenerator: + generated_metadata = namespace["metadata"] + elif generator_class is SQLModelGenerator: + generated_metadata = namespace["SQLModel"].metadata + else: + generated_metadata = namespace["Base"].metadata + + configure_mappers() + for table in generated_metadata.tables.values(): + generated_domain = table.c.value.type + assert generated_domain.metadata is generated_metadata + assert generated_domain.schema == schema + assert str(generated_domain.check) == "VALUE > 0" + + generated_metadata.create_all(engine, checkfirst=False) + assert sum(s.startswith("CREATE DOMAIN") for s in statements) == 1 + assert sum(s.startswith("CREATE TABLE") for s in statements) == 2 + statements.clear() + generated_metadata.drop_all(engine, checkfirst=False) + assert sum(s.startswith("DROP DOMAIN") for s in statements) == 1 + assert sum(s.startswith("DROP TABLE") for s in statements) == 2 + finally: + clear_mappers() + bind.dispose() + if generator_class is SQLModelGenerator and generated_metadata is not None: + for table in list(generated_metadata.tables.values()): + generated_metadata.remove(table) diff --git a/tests/test_generator_declarative.py b/tests/test_generator_declarative.py index a0a1d503..1d1ded8d 100644 --- a/tests/test_generator_declarative.py +++ b/tests/test_generator_declarative.py @@ -25,7 +25,7 @@ from sqlacodegen.generators import CodeGenerator, DeclarativeGenerator -from .conftest import validate_code +from .conftest import SQLALCHEMY_21, validate_code @pytest.fixture @@ -2444,6 +2444,8 @@ class WithItems(Base): @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) def test_domain_json(generator: CodeGenerator) -> None: + python_type = "object" if SQLALCHEMY_21 else "dict" + metadata_args = ", schema=None, metadata=Base.metadata" if SQLALCHEMY_21 else "" Table( "test_domain_json", generator.metadata, @@ -2476,8 +2478,8 @@ class TestDomainJson(Base): __tablename__ = 'test_domain_json' id: Mapped[int] = mapped_column(BigInteger, primary_key=True) - foo: Mapped[Optional[dict]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=False)) -""", + foo: Mapped[Optional[PYTHON_TYPE]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=FalseMETADATA_ARGS)) +""".replace("PYTHON_TYPE", python_type).replace("METADATA_ARGS", metadata_args), ) @@ -2489,6 +2491,8 @@ def test_domain_non_default_json( generator: CodeGenerator, domain_type: type[JSON] | type[JSONB], ) -> None: + python_type = "object" if SQLALCHEMY_21 else "dict" + metadata_args = ", schema=None, metadata=Base.metadata" if SQLALCHEMY_21 else "" Table( "test_domain_json", generator.metadata, @@ -2521,7 +2525,7 @@ class TestDomainJson(Base): __tablename__ = 'test_domain_json' id: Mapped[int] = mapped_column(BigInteger, primary_key=True) - foo: Mapped[Optional[dict]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False)) + foo: Mapped[Optional[{python_type}]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False{metadata_args})) """, ) @@ -2532,6 +2536,8 @@ class TestDomainJson(Base): ) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) def test_geoalchemy2_types(generator: CodeGenerator) -> None: + python_type = "object" if SQLALCHEMY_21 else "Any" + typing_import = "Optional" if SQLALCHEMY_21 else "Any, Optional" Table( "spatial_table", generator.metadata, @@ -2543,7 +2549,7 @@ def test_geoalchemy2_types(generator: CodeGenerator) -> None: validate_code( generator.generate(), """\ -from typing import Any, Optional +from typing import TYPING_IMPORT from geoalchemy2.types import Geography, Geometry from sqlalchemy import Index, Integer @@ -2561,9 +2567,9 @@ class SpatialTable(Base): ) id: Mapped[int] = mapped_column(Integer, primary_key=True) - geom: Mapped[Any] = mapped_column(Geometry('POINT', 4326, 2, from_text='ST_GeomFromEWKT', name='geometry', nullable=False), nullable=False) - geog: Mapped[Optional[Any]] = mapped_column(Geography('POLYGON', dimension=2, from_text='ST_GeogFromText', name='geography')) -""", + geom: Mapped[PYTHON_TYPE] = mapped_column(Geometry('POINT', 4326, 2, from_text='ST_GeomFromEWKT', name='geometry', nullable=False), nullable=False) + geog: Mapped[Optional[PYTHON_TYPE]] = mapped_column(Geography('POLYGON', dimension=2, from_text='ST_GeogFromText', name='geography')) +""".replace("TYPING_IMPORT", typing_import).replace("PYTHON_TYPE", python_type), ) diff --git a/tests/test_generator_tables.py b/tests/test_generator_tables.py index e467dc2b..7b726f14 100644 --- a/tests/test_generator_tables.py +++ b/tests/test_generator_tables.py @@ -27,7 +27,7 @@ from sqlacodegen.generators import CodeGenerator, TablesGenerator -from .conftest import validate_code +from .conftest import SQLALCHEMY_21, validate_code # This needs to be uppercased to trigger #315 @@ -506,6 +506,7 @@ class TagEnum(str, enum.Enum): @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) def test_domain_text(generator: CodeGenerator) -> None: + metadata_args = ", schema=None, metadata=metadata" if SQLALCHEMY_21 else "" Table( "simple_items", generator.metadata, @@ -535,14 +536,15 @@ def test_domain_text(generator: CodeGenerator) -> None: 'simple_items', metadata, Column('postal_code', DOMAIN('us_postal_code', Text(), \ constraint_name='valid_us_postal_code', not_null=False, \ -check=text("VALUE ~ '^\\\\d{5}$' OR VALUE ~ '^\\\\d{5}-\\\\d{4}$'")), nullable=False) +check=text("VALUE ~ '^\\\\d{5}$' OR VALUE ~ '^\\\\d{5}-\\\\d{4}$'")METADATA_ARGS), nullable=False) ) - """, + """.replace("METADATA_ARGS", metadata_args), ) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) def test_domain_int(generator: CodeGenerator) -> None: + metadata_args = ", schema=None, metadata=metadata" if SQLALCHEMY_21 else "" Table( "simple_items", generator.metadata, @@ -572,9 +574,9 @@ def test_domain_int(generator: CodeGenerator) -> None: 'simple_items', metadata, Column('n', DOMAIN('positive_int', INTEGER(), \ constraint_name='positive', not_null=False, \ -check=text('VALUE > 0')), nullable=False) +check=text('VALUE > 0')METADATA_ARGS), nullable=False) ) - """, + """.replace("METADATA_ARGS", metadata_args), ) From 62ac56ff6c084df1ba1631c69771e8e63a6812af Mon Sep 17 00:00:00 2001 From: Claire-ovo <132752951+Claire-ovo@users.noreply.github.com> Date: Sun, 25 Oct 2026 11:41:27 +0000 Subject: [PATCH 03/11] Separate SQLAlchemy compatibility and test both minor versions --- .github/workflows/test.yml | 10 +- CHANGES.rst | 3 - src/sqlacodegen/generators.py | 51 +++----- tests/conftest.py | 7 +- tests/test_generator_declarative.py | 177 +++++++++++++++++++++++++--- tests/test_generator_tables.py | 102 ++++++++++++++-- tests/test_nested_types.py | 89 -------------- 7 files changed, 287 insertions(+), 152 deletions(-) delete mode 100644 tests/test_nested_types.py diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 29da7583..ceb2e06c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -7,10 +7,16 @@ on: jobs: test: + name: Python ${{ matrix.python-version }} / SQLAlchemy ${{ matrix.sqlalchemy }} strategy: fail-fast: false matrix: python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"] + sqlalchemy: [">=2.0.29,<2.1", ">=2.1,<2.2"] + exclude: + # SQLAlchemy 2.1 requires Python 3.11 or later. + - python-version: "3.10" + sqlalchemy: ">=2.1,<2.2" runs-on: ubuntu-latest steps: - uses: actions/checkout@v7 @@ -22,7 +28,9 @@ jobs: cache: pip cache-dependency-path: pyproject.toml - name: Install dependencies - run: pip install --group test -e .[sqlmodel,citext,geoalchemy2,pgvector] + run: pip install --group test -e .[sqlmodel,citext,geoalchemy2,pgvector] "SQLAlchemy${{ matrix.sqlalchemy }}" + - name: Show dependency version + run: python -c "import sqlalchemy; print(sqlalchemy.__version__)" - name: Test with pytest run: coverage run -m pytest - name: Upload Coverage diff --git a/CHANGES.rst b/CHANGES.rst index 8a2dbcc4..b406263f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,9 +3,6 @@ Version history **UNRELEASED** -- Fixed rendering and imports of nested type classes and instances, including - ClickHouse array element types - (`#469 `_; PR by @Claire-ovo) - Fixed named types referencing a separate metadata instance in generated code, including PostgreSQL domains attached automatically by SQLAlchemy 2.1 - **BACKWARD INCOMPATIBLE** The SQLModel generator no longer emits ``t_`` diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index aa9a6fb9..99e3a25a 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -571,16 +571,17 @@ def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> s return render_callable("mapped_column", *args, kwargs=kwargs) def _render_column_type_value(self, value: Any) -> str: - if isinstance(value, type) and issubclass(value, TypeEngine): - self.add_import(value) - return value.__name__ - elif isinstance(value, TypeEngine): - self.add_import(value) - return self._render_type(value, nested=True) - elif isinstance(value, MetaData): + if isinstance(value, MetaData): # Named types must share the generated tables' DDL lifecycle. return self.base.metadata_ref + if isinstance(value, (JSONB, JSON)): + # Remove astext_type if it's the default + if isinstance(value.astext_type, Text) and value.astext_type.length is None: + value.astext_type = None # type: ignore[assignment] + else: + self.add_import(Text) + if isinstance(value, TextClause): self.add_literal_import("sqlalchemy", "text") return render_callable("text", repr(value.text)) @@ -662,6 +663,7 @@ def render_column_type(self, column: Column[Any]) -> str: return f"Enum({enum_class_name}, values_callable=lambda cls: [member.value for member in cls]{extra_kwargs})" + args = [] kwargs: dict[str, Any] = {} # Check if this is an ARRAY column with an Enum item type mapped to a Python enum class @@ -684,13 +686,6 @@ def render_column_type(self, column: Column[Any]) -> str: return render_callable("ARRAY", rendered_enum, kwargs=kwargs) - return self._render_type(column_type) - - def _render_type( - self, column_type: TypeEngine[Any], *, nested: bool = False - ) -> str: - args = [] - kwargs: dict[str, Any] = {} sig = inspect.signature(column_type.__class__.__init__) defaults = {param.name: param.default for param in sig.parameters.values()} missing = object() @@ -712,19 +707,6 @@ def _render_type( use_kwargs = True continue - if ( - param.name == "astext_type" - and isinstance(column_type, (JSONB, JSON)) - and isinstance(value, Text) - and value.length is None - ): - use_kwargs = True - continue - - if nested and param.default is not Parameter.empty: - # Preserve the keyword style previously supplied by type reprs. - use_kwargs = True - rendered_value = self._render_column_type_value(value) if use_kwargs: kwargs[param.name] = rendered_value @@ -746,10 +728,7 @@ def _render_type( None, ) if vararg and hasattr(column_type, vararg): - varargs_repr = [ - self._render_column_type_value(arg) - for arg in getattr(column_type, vararg) - ] + varargs_repr = [repr(arg) for arg in getattr(column_type, vararg)] args.extend(varargs_repr) # These arguments cannot be autodetected from the Enum initializer @@ -758,7 +737,15 @@ def _render_type( if (value := getattr(column_type, colname)) is not None: kwargs[colname] = repr(value) - if args or kwargs or nested: + if isinstance(column_type, (JSONB, JSON)): + # Remove astext_type if it's the default + if ( + isinstance(column_type.astext_type, Text) + and column_type.astext_type.length is None + ): + del kwargs["astext_type"] + + if args or kwargs: return render_callable(column_type.__class__.__name__, *args, kwargs=kwargs) else: return column_type.__class__.__name__ diff --git a/tests/conftest.py b/tests/conftest.py index 75626330..a802d129 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -7,7 +7,12 @@ from sqlalchemy.orm import clear_mappers, configure_mappers from sqlalchemy.schema import MetaData -SQLALCHEMY_21 = tuple(map(int, sqlalchemy.__version__.split(".")[:2])) >= (2, 1) + +@pytest.fixture +def sqlalchemy_version(request: FixtureRequest) -> None: + installed = tuple(map(int, sqlalchemy.__version__.split(".")[:2])) + if installed != request.param: + pytest.skip(f"Requires SQLAlchemy {request.param}; installed {installed}") @pytest.fixture diff --git a/tests/test_generator_declarative.py b/tests/test_generator_declarative.py index 1d1ded8d..42062ff5 100644 --- a/tests/test_generator_declarative.py +++ b/tests/test_generator_declarative.py @@ -25,7 +25,7 @@ from sqlacodegen.generators import CodeGenerator, DeclarativeGenerator -from .conftest import SQLALCHEMY_21, validate_code +from .conftest import validate_code @pytest.fixture @@ -2442,10 +2442,11 @@ class WithItems(Base): ) +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] +) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_domain_json(generator: CodeGenerator) -> None: - python_type = "object" if SQLALCHEMY_21 else "dict" - metadata_args = ", schema=None, metadata=Base.metadata" if SQLALCHEMY_21 else "" +def test_domain_json_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> None: Table( "test_domain_json", generator.metadata, @@ -2478,21 +2479,114 @@ class TestDomainJson(Base): __tablename__ = 'test_domain_json' id: Mapped[int] = mapped_column(BigInteger, primary_key=True) - foo: Mapped[Optional[PYTHON_TYPE]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=FalseMETADATA_ARGS)) -""".replace("PYTHON_TYPE", python_type).replace("METADATA_ARGS", metadata_args), + foo: Mapped[Optional[dict]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=False)) +""", + ) + + +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] +) +@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) +def test_domain_json_sa21(sqlalchemy_version: None, generator: CodeGenerator) -> None: + Table( + "test_domain_json", + generator.metadata, + Column("id", BIGINT, primary_key=True), + Column( + "foo", + postgresql.DOMAIN( + "domain_json", + JSON, + not_null=False, + ), + nullable=True, + ), + ) + + validate_code( + generator.generate(), + """\ +from typing import Optional + +from sqlalchemy import BigInteger +from sqlalchemy.dialects.postgresql import DOMAIN, JSON +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column + +class Base(DeclarativeBase): + pass + + +class TestDomainJson(Base): + __tablename__ = 'test_domain_json' + + id: Mapped[int] = mapped_column(BigInteger, primary_key=True) + foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=False, schema=None, metadata=Base.metadata)) +""", + ) + + +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] +) +@pytest.mark.parametrize( + "domain_type", + [JSONB, JSON], +) +def test_domain_non_default_json_sa20( + sqlalchemy_version: None, + generator: CodeGenerator, + domain_type: type[JSON] | type[JSONB], +) -> None: + Table( + "test_domain_json", + generator.metadata, + Column("id", BIGINT, primary_key=True), + Column( + "foo", + postgresql.DOMAIN( + "domain_json", + domain_type(astext_type=Text(128)), + not_null=False, + ), + nullable=True, + ), + ) + + validate_code( + generator.generate(), + f"""\ +from typing import Optional + +from sqlalchemy import BigInteger, Text +from sqlalchemy.dialects.postgresql import DOMAIN, {domain_type.__name__} +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column + +class Base(DeclarativeBase): + pass + + +class TestDomainJson(Base): + __tablename__ = 'test_domain_json' + + id: Mapped[int] = mapped_column(BigInteger, primary_key=True) + foo: Mapped[Optional[dict]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False)) +""", ) +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] +) @pytest.mark.parametrize( "domain_type", [JSONB, JSON], ) -def test_domain_non_default_json( +def test_domain_non_default_json_sa21( + sqlalchemy_version: None, generator: CodeGenerator, domain_type: type[JSON] | type[JSONB], ) -> None: - python_type = "object" if SQLALCHEMY_21 else "dict" - metadata_args = ", schema=None, metadata=Base.metadata" if SQLALCHEMY_21 else "" Table( "test_domain_json", generator.metadata, @@ -2525,19 +2619,22 @@ class TestDomainJson(Base): __tablename__ = 'test_domain_json' id: Mapped[int] = mapped_column(BigInteger, primary_key=True) - foo: Mapped[Optional[{python_type}]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False{metadata_args})) + foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False, schema=None, metadata=Base.metadata)) """, ) +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] +) @pytest.mark.skipif( sys.version_info < (3, 10), reason="This test assumes GeoAlchemy2 0.18.x and above, which does not support python 3.9", ) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_geoalchemy2_types(generator: CodeGenerator) -> None: - python_type = "object" if SQLALCHEMY_21 else "Any" - typing_import = "Optional" if SQLALCHEMY_21 else "Any, Optional" +def test_geoalchemy2_types_sa20( + sqlalchemy_version: None, generator: CodeGenerator +) -> None: Table( "spatial_table", generator.metadata, @@ -2549,7 +2646,7 @@ def test_geoalchemy2_types(generator: CodeGenerator) -> None: validate_code( generator.generate(), """\ -from typing import TYPING_IMPORT +from typing import Any, Optional from geoalchemy2.types import Geography, Geometry from sqlalchemy import Index, Integer @@ -2567,9 +2664,55 @@ class SpatialTable(Base): ) id: Mapped[int] = mapped_column(Integer, primary_key=True) - geom: Mapped[PYTHON_TYPE] = mapped_column(Geometry('POINT', 4326, 2, from_text='ST_GeomFromEWKT', name='geometry', nullable=False), nullable=False) - geog: Mapped[Optional[PYTHON_TYPE]] = mapped_column(Geography('POLYGON', dimension=2, from_text='ST_GeogFromText', name='geography')) -""".replace("TYPING_IMPORT", typing_import).replace("PYTHON_TYPE", python_type), + geom: Mapped[Any] = mapped_column(Geometry('POINT', 4326, 2, from_text='ST_GeomFromEWKT', name='geometry', nullable=False), nullable=False) + geog: Mapped[Optional[Any]] = mapped_column(Geography('POLYGON', dimension=2, from_text='ST_GeogFromText', name='geography')) +""", + ) + + +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] +) +@pytest.mark.skipif( + sys.version_info < (3, 10), + reason="This test assumes GeoAlchemy2 0.18.x and above, which does not support python 3.9", +) +@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) +def test_geoalchemy2_types_sa21( + sqlalchemy_version: None, generator: CodeGenerator +) -> None: + Table( + "spatial_table", + generator.metadata, + Column("id", INTEGER, primary_key=True), + Column("geom", Geometry("POINT", srid=4326, dimension=2), nullable=False), + Column("geog", Geography("POLYGON", dimension=2)), + ) + + validate_code( + generator.generate(), + """\ +from typing import Optional + +from geoalchemy2.types import Geography, Geometry +from sqlalchemy import Index, Integer +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column + +class Base(DeclarativeBase): + pass + + +class SpatialTable(Base): + __tablename__ = 'spatial_table' + __table_args__ = ( + Index('idx_spatial_table_geog', 'geog', postgresql_using='gist'), + Index('idx_spatial_table_geom', 'geom', postgresql_using='gist') + ) + + id: Mapped[int] = mapped_column(Integer, primary_key=True) + geom: Mapped[object] = mapped_column(Geometry('POINT', 4326, 2, from_text='ST_GeomFromEWKT', name='geometry', nullable=False), nullable=False) + geog: Mapped[Optional[object]] = mapped_column(Geography('POLYGON', dimension=2, from_text='ST_GeogFromText', name='geography')) +""", ) diff --git a/tests/test_generator_tables.py b/tests/test_generator_tables.py index 7b726f14..d9fb30d2 100644 --- a/tests/test_generator_tables.py +++ b/tests/test_generator_tables.py @@ -27,7 +27,7 @@ from sqlacodegen.generators import CodeGenerator, TablesGenerator -from .conftest import SQLALCHEMY_21, validate_code +from .conftest import validate_code # This needs to be uppercased to trigger #315 @@ -504,9 +504,51 @@ class TagEnum(str, enum.Enum): ) +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] +) +@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) +def test_domain_text_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> None: + Table( + "simple_items", + generator.metadata, + Column( + "postal_code", + postgresql.DOMAIN( + "us_postal_code", + Text, + constraint_name="valid_us_postal_code", + not_null=False, + check=text("VALUE ~ '^\\d{5}$' OR VALUE ~ '^\\d{5}-\\d{4}$'"), + ), + nullable=False, + ), + ) + + validate_code( + generator.generate(), + """\ + from sqlalchemy import Column, MetaData, Table, Text, text + from sqlalchemy.dialects.postgresql import DOMAIN + + metadata = MetaData() + + + t_simple_items = Table( + 'simple_items', metadata, + Column('postal_code', DOMAIN('us_postal_code', Text(), \ +constraint_name='valid_us_postal_code', not_null=False, \ +check=text("VALUE ~ '^\\\\d{5}$' OR VALUE ~ '^\\\\d{5}-\\\\d{4}$'")), nullable=False) + ) + """, + ) + + +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] +) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_domain_text(generator: CodeGenerator) -> None: - metadata_args = ", schema=None, metadata=metadata" if SQLALCHEMY_21 else "" +def test_domain_text_sa21(sqlalchemy_version: None, generator: CodeGenerator) -> None: Table( "simple_items", generator.metadata, @@ -536,15 +578,57 @@ def test_domain_text(generator: CodeGenerator) -> None: 'simple_items', metadata, Column('postal_code', DOMAIN('us_postal_code', Text(), \ constraint_name='valid_us_postal_code', not_null=False, \ -check=text("VALUE ~ '^\\\\d{5}$' OR VALUE ~ '^\\\\d{5}-\\\\d{4}$'")METADATA_ARGS), nullable=False) +check=text("VALUE ~ '^\\\\d{5}$' OR VALUE ~ '^\\\\d{5}-\\\\d{4}$'"), schema=None, metadata=metadata), nullable=False) + ) + """, + ) + + +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] +) +@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) +def test_domain_int_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> None: + Table( + "simple_items", + generator.metadata, + Column( + "n", + postgresql.DOMAIN( + "positive_int", + INTEGER, + constraint_name="positive", + not_null=False, + check=text("VALUE > 0"), + ), + nullable=False, + ), + ) + + validate_code( + generator.generate(), + """\ + from sqlalchemy import Column, INTEGER, MetaData, Table, text + from sqlalchemy.dialects.postgresql import DOMAIN + + metadata = MetaData() + + + t_simple_items = Table( + 'simple_items', metadata, + Column('n', DOMAIN('positive_int', INTEGER(), \ +constraint_name='positive', not_null=False, \ +check=text('VALUE > 0')), nullable=False) ) - """.replace("METADATA_ARGS", metadata_args), + """, ) +@pytest.mark.parametrize( + "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] +) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_domain_int(generator: CodeGenerator) -> None: - metadata_args = ", schema=None, metadata=metadata" if SQLALCHEMY_21 else "" +def test_domain_int_sa21(sqlalchemy_version: None, generator: CodeGenerator) -> None: Table( "simple_items", generator.metadata, @@ -574,9 +658,9 @@ def test_domain_int(generator: CodeGenerator) -> None: 'simple_items', metadata, Column('n', DOMAIN('positive_int', INTEGER(), \ constraint_name='positive', not_null=False, \ -check=text('VALUE > 0')METADATA_ARGS), nullable=False) +check=text('VALUE > 0'), schema=None, metadata=metadata), nullable=False) ) - """.replace("METADATA_ARGS", metadata_args), + """, ) diff --git a/tests/test_nested_types.py b/tests/test_nested_types.py deleted file mode 100644 index 5de70227..00000000 --- a/tests/test_nested_types.py +++ /dev/null @@ -1,89 +0,0 @@ -from __future__ import annotations - -from typing import Any - -import pytest -from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine -from sqlalchemy.orm import clear_mappers, configure_mappers -from sqlalchemy.types import TypeEngine, UserDefinedType - -from sqlacodegen.generators import ( - DataclassGenerator, - DeclarativeGenerator, - SQLModelGenerator, - TablesGenerator, -) - - -class WrappedType(UserDefinedType[list[Any]]): - """Like ClickHouse Array, retain either a type class or an instance.""" - - def __init__(self, item_type: TypeEngine[Any] | type[TypeEngine[Any]]) -> None: - self.item_type = item_type - - @property - def python_type(self) -> type[list[Any]]: - return list - - def get_col_spec(self, **kwargs: Any) -> str: - return "WRAPPED" - - -class TupleType(UserDefinedType[tuple[Any, ...]]): - def __init__(self, *types: TypeEngine[Any] | type[TypeEngine[Any]]) -> None: - self.types = types - - @property - def python_type(self) -> type[tuple[Any, ...]]: - return tuple - - def get_col_spec(self, **kwargs: Any) -> str: - return f"TUPLE({', '.join(str(type_) for type_ in self.types)})" - - -@pytest.mark.parametrize( - "generator_class", - [TablesGenerator, DeclarativeGenerator, DataclassGenerator, SQLModelGenerator], -) -@pytest.mark.parametrize("options", [[], ["keep_dialect_types"]]) -def test_nested_type_arguments( - generator_class: type[TablesGenerator], options: list[str] -) -> None: - metadata = MetaData() - Table( - "nested_types", - metadata, - Column("id", Integer, primary_key=True), - Column("type_class", WrappedType(String), nullable=False), - Column("instance", WrappedType(String()), nullable=False), - Column("parameterized", WrappedType(String(24)), nullable=False), - Column("nested", WrappedType(WrappedType(String)), nullable=False), - Column("variadic", TupleType(Integer, String(12)), nullable=False), - ) - engine = create_engine("sqlite://") - namespace: dict[str, Any] = {} - table = None - try: - code = generator_class(metadata, engine, options).generate() - exec(compile(code, "", "exec", dont_inherit=True), namespace) - if generator_class is TablesGenerator: - table = namespace["t_nested_types"] - else: - table = namespace["NestedTypes"].__table__ - - configure_mappers() - - assert table.c.type_class.type.item_type is String - assert isinstance(table.c.instance.type.item_type, String) - assert table.c.instance.type.item_type.length is None - assert table.c.parameterized.type.item_type.length == 24 - assert isinstance(table.c.nested.type.item_type, WrappedType) - assert table.c.nested.type.item_type.item_type is String - assert table.c.variadic.type.types[0] is Integer - assert isinstance(table.c.variadic.type.types[1], String) - assert table.c.variadic.type.types[1].length == 12 - finally: - clear_mappers() - if generator_class is SQLModelGenerator and table is not None: - table.metadata.remove(table) - engine.dispose() From cfa486d87127aa7414dc8309e81e425443dbec66 Mon Sep 17 00:00:00 2001 From: Idan Sheinberg Date: Mon, 9 Nov 2026 13:33:23 +0200 Subject: [PATCH 04/11] Reword changelog entry for SQLAlchemy 2.1 support and link PR #489 Co-Authored-By: Claude Fable 5.1 --- CHANGES.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index b406263f..41a831df 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,8 +3,10 @@ Version history **UNRELEASED** -- Fixed named types referencing a separate metadata instance in generated code, - including PostgreSQL domains attached automatically by SQLAlchemy 2.1 +- Added SQLAlchemy 2.1 support: named types (such as PostgreSQL domains, which + SQLAlchemy 2.1 attaches to the table metadata automatically) no longer reference a + separate metadata instance in generated code + (`#489 `_; PR by @Claire-ovo) - **BACKWARD INCOMPATIBLE** The SQLModel generator no longer emits ``t_`` ``Table`` variables for many-to-many association tables that have a primary key, and their relationships no longer pass ``secondary`` via ``sa_relationship_kwargs``. From 05f90bfb8e5e0bf683c3c2fc42dfd7d0b3387830 Mon Sep 17 00:00:00 2001 From: Idan Sheinberg Date: Wed, 18 Nov 2026 21:02:38 +0200 Subject: [PATCH 05/11] Remove sub python 3.10 tests new tests --- tests/test_generator_declarative.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/tests/test_generator_declarative.py b/tests/test_generator_declarative.py index 42062ff5..3ead9c61 100644 --- a/tests/test_generator_declarative.py +++ b/tests/test_generator_declarative.py @@ -1,7 +1,5 @@ from __future__ import annotations -import sys - import pytest from _pytest.fixtures import FixtureRequest from geoalchemy2 import Geography, Geometry @@ -2627,10 +2625,6 @@ class TestDomainJson(Base): @pytest.mark.parametrize( "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] ) -@pytest.mark.skipif( - sys.version_info < (3, 10), - reason="This test assumes GeoAlchemy2 0.18.x and above, which does not support python 3.9", -) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) def test_geoalchemy2_types_sa20( sqlalchemy_version: None, generator: CodeGenerator @@ -2673,10 +2667,6 @@ class SpatialTable(Base): @pytest.mark.parametrize( "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] ) -@pytest.mark.skipif( - sys.version_info < (3, 10), - reason="This test assumes GeoAlchemy2 0.18.x and above, which does not support python 3.9", -) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) def test_geoalchemy2_types_sa21( sqlalchemy_version: None, generator: CodeGenerator From 1cba8c6bb44878b120365ccd47437f89e7c31c9e Mon Sep 17 00:00:00 2001 From: Idan Sheinberg Date: Wed, 18 Nov 2026 21:10:19 +0200 Subject: [PATCH 06/11] Remove the standalone domain metadata test The SQLAlchemy 2.1 behavior is already covered by the version-parametrized generator tests. Co-Authored-By: Claude Fable 5.2 --- tests/test_domain_metadata.py | 92 ----------------------------------- 1 file changed, 92 deletions(-) delete mode 100644 tests/test_domain_metadata.py diff --git a/tests/test_domain_metadata.py b/tests/test_domain_metadata.py deleted file mode 100644 index c4262b68..00000000 --- a/tests/test_domain_metadata.py +++ /dev/null @@ -1,92 +0,0 @@ -from __future__ import annotations - -from typing import Any - -import pytest -from sqlalchemy import ( - Column, - Integer, - MetaData, - Table, - create_engine, - create_mock_engine, -) -from sqlalchemy.dialects.postgresql import DOMAIN -from sqlalchemy.orm import clear_mappers, configure_mappers -from sqlmodel import SQLModel - -from sqlacodegen.generators import ( - DataclassGenerator, - DeclarativeGenerator, - SQLModelGenerator, - TablesGenerator, -) - - -@pytest.mark.parametrize( - "generator_class", - [TablesGenerator, DeclarativeGenerator, DataclassGenerator, SQLModelGenerator], -) -@pytest.mark.parametrize("options", [[], ["keep_dialect_types"]]) -@pytest.mark.parametrize("schema", [None, "domain_types"]) -def test_domain_metadata( - generator_class: type[TablesGenerator], - options: list[str], - schema: str | None, - monkeypatch: pytest.MonkeyPatch, -) -> None: - # Isolate named-type DDL listeners as well as tables between SQLModel cases. - if generator_class is SQLModelGenerator: - monkeypatch.setattr(SQLModel, "metadata", MetaData()) - metadata = MetaData() - domain = DOMAIN( - "positive_number", Integer, metadata=metadata, schema=schema, check="VALUE > 0" - ) - for name in ("first_item", "second_item"): - Table( - name, - metadata, - Column("id", Integer, primary_key=True), - Column("value", domain, nullable=False), - schema="items", - ) - - statements: list[str] = [] - - def execute(statement: Any, *args: Any, **kwargs: Any) -> None: - statements.append(str(statement.compile(dialect=engine.dialect)).strip()) - - engine = create_mock_engine("postgresql://", execute) - bind = create_engine("postgresql+psycopg://") - namespace: dict[str, Any] = {} - generated_metadata = None - try: - code = generator_class(metadata, bind, options).generate() - exec(compile(code, "", "exec", dont_inherit=True), namespace) - if generator_class is TablesGenerator: - generated_metadata = namespace["metadata"] - elif generator_class is SQLModelGenerator: - generated_metadata = namespace["SQLModel"].metadata - else: - generated_metadata = namespace["Base"].metadata - - configure_mappers() - for table in generated_metadata.tables.values(): - generated_domain = table.c.value.type - assert generated_domain.metadata is generated_metadata - assert generated_domain.schema == schema - assert str(generated_domain.check) == "VALUE > 0" - - generated_metadata.create_all(engine, checkfirst=False) - assert sum(s.startswith("CREATE DOMAIN") for s in statements) == 1 - assert sum(s.startswith("CREATE TABLE") for s in statements) == 2 - statements.clear() - generated_metadata.drop_all(engine, checkfirst=False) - assert sum(s.startswith("DROP DOMAIN") for s in statements) == 1 - assert sum(s.startswith("DROP TABLE") for s in statements) == 2 - finally: - clear_mappers() - bind.dispose() - if generator_class is SQLModelGenerator and generated_metadata is not None: - for table in list(generated_metadata.tables.values()): - generated_metadata.remove(table) From ad630f745e07054624a56ecd778c13d4d8c75429 Mon Sep 17 00:00:00 2001 From: Idan Sheinberg Date: Wed, 18 Nov 2026 21:10:19 +0200 Subject: [PATCH 07/11] Drop the SQLAlchemy < 2.1 upper bound The pin made the SQLAlchemy 2.1 CI jobs fail at dependency resolution. Co-Authored-By: Claude Fable 5.2 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 009ba0d3..1da5f146 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -29,7 +29,7 @@ classifiers = [ ] requires-python = ">=3.10" dependencies = [ - "SQLAlchemy >= 2.0.29, < 2.1", + "SQLAlchemy >= 2.0.29", "inflect >= 4.0.0", ] dynamic = ["version"] From f992ad93bb185c6d37e8df87f08fa825bed9b3b7 Mon Sep 17 00:00:00 2001 From: Idan Sheinberg Date: Wed, 18 Nov 2026 21:13:58 +0200 Subject: [PATCH 08/11] Stop rendering schema and metadata for named types on SQLAlchemy 2.1 SQLAlchemy 2.1 uses a sentinel default for SchemaType's schema argument and sets the table's metadata on the type when its column is attached to a table, which made both look like non-default arguments. Neither is needed in the generated code, so the output for the plain domain tests is the same on 2.0 and 2.1 again and their version-specific copies are gone. Co-Authored-By: Claude Fable 5.2 --- CHANGES.rst | 4 +- src/sqlacodegen/generators.py | 9 +-- tests/test_generator_declarative.py | 4 +- tests/test_generator_tables.py | 90 +---------------------------- 4 files changed, 10 insertions(+), 97 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 41a831df..da574bbf 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,9 +3,7 @@ Version history **UNRELEASED** -- Added SQLAlchemy 2.1 support: named types (such as PostgreSQL domains, which - SQLAlchemy 2.1 attaches to the table metadata automatically) no longer reference a - separate metadata instance in generated code +- Added SQLAlchemy 2.1 support (`#489 `_; PR by @Claire-ovo) - **BACKWARD INCOMPATIBLE** The SQLModel generator no longer emits ``t_`` ``Table`` variables for many-to-many association tables that have a primary key, and diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index 99e3a25a..b6f79de5 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -571,10 +571,6 @@ def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> s return render_callable("mapped_column", *args, kwargs=kwargs) def _render_column_type_value(self, value: Any) -> str: - if isinstance(value, MetaData): - # Named types must share the generated tables' DDL lifecycle. - return self.base.metadata_ref - if isinstance(value, (JSONB, JSON)): # Remove astext_type if it's the default if isinstance(value.astext_type, Text) and value.astext_type.length is None: @@ -639,6 +635,11 @@ def _collect_inherited_init_kwargs( if default is not Parameter.empty and value == default: continue + # SQLAlchemy 2.1 attaches the table's metadata to named types and + # uses a sentinel as the default for "schema" + if value is None or isinstance(value, MetaData): + continue + inherited_kwargs[super_param.name] = self._render_column_type_value( value ) diff --git a/tests/test_generator_declarative.py b/tests/test_generator_declarative.py index 3ead9c61..f370c99c 100644 --- a/tests/test_generator_declarative.py +++ b/tests/test_generator_declarative.py @@ -2519,7 +2519,7 @@ class TestDomainJson(Base): __tablename__ = 'test_domain_json' id: Mapped[int] = mapped_column(BigInteger, primary_key=True) - foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=False, schema=None, metadata=Base.metadata)) + foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=False)) """, ) @@ -2617,7 +2617,7 @@ class TestDomainJson(Base): __tablename__ = 'test_domain_json' id: Mapped[int] = mapped_column(BigInteger, primary_key=True) - foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False, schema=None, metadata=Base.metadata)) + foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False)) """, ) diff --git a/tests/test_generator_tables.py b/tests/test_generator_tables.py index d9fb30d2..e467dc2b 100644 --- a/tests/test_generator_tables.py +++ b/tests/test_generator_tables.py @@ -504,11 +504,8 @@ class TagEnum(str, enum.Enum): ) -@pytest.mark.parametrize( - "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] -) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_domain_text_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> None: +def test_domain_text(generator: CodeGenerator) -> None: Table( "simple_items", generator.metadata, @@ -544,51 +541,8 @@ def test_domain_text_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> ) -@pytest.mark.parametrize( - "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] -) -@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_domain_text_sa21(sqlalchemy_version: None, generator: CodeGenerator) -> None: - Table( - "simple_items", - generator.metadata, - Column( - "postal_code", - postgresql.DOMAIN( - "us_postal_code", - Text, - constraint_name="valid_us_postal_code", - not_null=False, - check=text("VALUE ~ '^\\d{5}$' OR VALUE ~ '^\\d{5}-\\d{4}$'"), - ), - nullable=False, - ), - ) - - validate_code( - generator.generate(), - """\ - from sqlalchemy import Column, MetaData, Table, Text, text - from sqlalchemy.dialects.postgresql import DOMAIN - - metadata = MetaData() - - - t_simple_items = Table( - 'simple_items', metadata, - Column('postal_code', DOMAIN('us_postal_code', Text(), \ -constraint_name='valid_us_postal_code', not_null=False, \ -check=text("VALUE ~ '^\\\\d{5}$' OR VALUE ~ '^\\\\d{5}-\\\\d{4}$'"), schema=None, metadata=metadata), nullable=False) - ) - """, - ) - - -@pytest.mark.parametrize( - "sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"] -) @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_domain_int_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> None: +def test_domain_int(generator: CodeGenerator) -> None: Table( "simple_items", generator.metadata, @@ -624,46 +578,6 @@ def test_domain_int_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> ) -@pytest.mark.parametrize( - "sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"] -) -@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) -def test_domain_int_sa21(sqlalchemy_version: None, generator: CodeGenerator) -> None: - Table( - "simple_items", - generator.metadata, - Column( - "n", - postgresql.DOMAIN( - "positive_int", - INTEGER, - constraint_name="positive", - not_null=False, - check=text("VALUE > 0"), - ), - nullable=False, - ), - ) - - validate_code( - generator.generate(), - """\ - from sqlalchemy import Column, INTEGER, MetaData, Table, text - from sqlalchemy.dialects.postgresql import DOMAIN - - metadata = MetaData() - - - t_simple_items = Table( - 'simple_items', metadata, - Column('n', DOMAIN('positive_int', INTEGER(), \ -constraint_name='positive', not_null=False, \ -check=text('VALUE > 0'), schema=None, metadata=metadata), nullable=False) - ) - """, - ) - - @pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"]) def test_column_adaptation(generator: CodeGenerator) -> None: Table( From 332f05a1270b178defbab0d793538c840a7b106a Mon Sep 17 00:00:00 2001 From: Idan Sheinberg Date: Wed, 18 Nov 2026 21:19:40 +0200 Subject: [PATCH 09/11] Move the SQLAlchemy 2.1 changelog entry and credit the rework Co-Authored-By: Claude Fable 5.2 --- CHANGES.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index da574bbf..c267088f 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -3,8 +3,6 @@ Version history **UNRELEASED** -- Added SQLAlchemy 2.1 support - (`#489 `_; PR by @Claire-ovo) - **BACKWARD INCOMPATIBLE** The SQLModel generator no longer emits ``t_`` ``Table`` variables for many-to-many association tables that have a primary key, and their relationships no longer pass ``secondary`` via ``sa_relationship_kwargs``. @@ -21,6 +19,8 @@ Version history - Fixed a column named ``registry`` being rendered as-is, which made SQLAlchemy warn that the attribute should be left reserved for the declarative class registry; it is now renamed to ``registry_`` the same way a ``metadata`` column already was +- Added SQLAlchemy 2.1 support + (`#489 `_; PR by @Claire-ovo, reworked by @sheinbergon) **4.0.4** From a6a97ed2e8992d6989d75e795299500cdfafb01b Mon Sep 17 00:00:00 2001 From: Idan Sheinberg Date: Wed, 18 Nov 2026 21:20:24 +0200 Subject: [PATCH 10/11] Wrap the SQLAlchemy 2.1 changelog entry Co-Authored-By: Claude Fable 5.2 --- CHANGES.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index c267088f..b6f1994d 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -20,7 +20,8 @@ Version history that the attribute should be left reserved for the declarative class registry; it is now renamed to ``registry_`` the same way a ``metadata`` column already was - Added SQLAlchemy 2.1 support - (`#489 `_; PR by @Claire-ovo, reworked by @sheinbergon) + (`#489 `_; PR by @Claire-ovo, + reworked by @sheinbergon) **4.0.4** From 9b7d0a7b0d7d468725c043eb4828cb98774d6b9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Gr=C3=B6nholm?= Date: Sat, 21 Nov 2026 02:06:16 +0200 Subject: [PATCH 11/11] Moved the changelog entry --- CHANGES.rst | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index e754593a..40533167 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,6 +11,9 @@ Version history their relationships no longer pass ``secondary`` via ``sa_relationship_kwargs``. Code importing those variables will break when models are regenerated. Use the ``nolinktables`` option to keep the previous output. +- Added SQLAlchemy 2.1 support + (`#489 `_; PR by @Claire-ovo, + reworked by @sheinbergon) - Added support for many-to-many link models in the SQLModel generator: association tables with a primary key are now rendered as ``SQLModel`` classes and referenced via ``Relationship(link_model=...)`` @@ -22,9 +25,6 @@ Version history - Fixed a column named ``registry`` being rendered as-is, which made SQLAlchemy warn that the attribute should be left reserved for the declarative class registry; it is now renamed to ``registry_`` the same way a ``metadata`` column already was -- Added SQLAlchemy 2.1 support - (`#489 `_; PR by @Claire-ovo, - reworked by @sheinbergon) **4.0.4**