From 15fc78c7369a26586e743960ffaeae6a12bc83a3 Mon Sep 17 00:00:00 2001 From: Jay Hemnani <193022578+jayhemnani9910@users.noreply.github.com> Date: Thu, 19 Nov 2026 03:35:07 +0530 Subject: [PATCH] Rename a column named like a module used in its annotation (uuid, datetime, decimal) --- CHANGES.rst | 4 ++++ src/sqlacodegen/generators.py | 7 ++++++- tests/test_generator_declarative.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGES.rst b/CHANGES.rst index 40e0b3cf..cbf9d157 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -19,6 +19,10 @@ 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 +- Fixed a column named like a module used in the type annotations (``uuid``, + ``datetime``, ``decimal``) shadowing that module, which made the generated declarative + and dataclass models fail at import with ``AttributeError``; the attribute is now renamed + (`#331 `_; PR by @jayhemnani9910) **4.0.4** diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index a9571572..3d6c8126 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -1148,6 +1148,11 @@ def collect_imports_for_model(self, model: Model) -> None: if model.relationships: self.add_literal_import("sqlalchemy.orm", "relationship") + # Collect the modules the Mapped[] annotations use (uuid, datetime, + # decimal) now, so attribute names can avoid them + for column in model.table.columns: + self.render_column_python_type(column) + def generate_models(self) -> list[Model]: models_by_table_name: dict[str, Model] = {} @@ -1223,7 +1228,7 @@ def generate_models(self) -> list[Model]: # attributes global_names = { name for namespace in self.imports.values() for name in namespace - } + } | self.module_imports for model in models_by_table_name.values(): self.generate_model_name(model, global_names) global_names.add(model.name) diff --git a/tests/test_generator_declarative.py b/tests/test_generator_declarative.py index a0a1d503..ee9fc804 100644 --- a/tests/test_generator_declarative.py +++ b/tests/test_generator_declarative.py @@ -2187,6 +2187,35 @@ class Simple(Base): ) +def test_column_named_like_annotation_module(generator: CodeGenerator) -> None: + Table( + "accounts", + generator.metadata, + Column("id", INTEGER, primary_key=True), + Column("uuid", postgresql.UUID, nullable=False), + ) + + validate_code( + generator.generate(), + """\ +import uuid + +from sqlalchemy import Integer, UUID +from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column + +class Base(DeclarativeBase): + pass + + +class Accounts(Base): + __tablename__ = 'accounts' + + id: Mapped[int] = mapped_column(Integer, primary_key=True) + uuid_: Mapped[uuid.UUID] = mapped_column('uuid', UUID, nullable=False) +""", + ) + + def test_invalid_variable_name_from_column(generator: CodeGenerator) -> None: Table( "simple",