Skip to content

Rename a column named like a module used in its annotation (uuid, datetime, decimal) - #492

Open
jayhemnani9910 wants to merge 1 commit into
agronholm:masterfrom
jayhemnani9910:fix/column-named-like-type-module
Open

jayhemnani9910 wants to merge 1 commit into
agronholm:masterfrom
jayhemnani9910:fix/column-named-like-type-module

Conversation

@jayhemnani9910

Copy link
Copy Markdown

Changes

Fixes #331 (closed as fixed, but it is back in 4.0.4 and master).

A table with a column named uuid of type uuid generates models that fail at import:

import uuid
...
    uuid: Mapped[uuid.UUID] = mapped_column(Uuid, nullable=False, ...)
# AttributeError: 'MappedColumn' object has no attribute 'UUID'

Inside the class body the attribute uuid shadows the uuid module used in the annotation. The same happens for a column named datetime or decimal, with the declarative and dataclass generators.

Attribute names are made unique against global_names, which only held the from x import y names. The modules used in the Mapped[] annotations were added later, while rendering. DeclarativeGenerator.collect_imports_for_model now renders the column python types while collecting imports, and the module imports are added to global_names, so such a column becomes uuid_ = mapped_column('uuid', ...), the same way metadata and registry are handled.

Checklist

  • You've added tests (in tests/) which would fail without your patch: test_column_named_like_annotation_module
  • You've added a new changelog entry (in CHANGES.rst).

pytest: 159 passed; the 6 tests/test_cli.py failures here (FileNotFoundError, the sqlacodegen script isn't on PATH in my venv) are the same without this change. Generated models for a Postgres table with a uuid column now import.

@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 97.858% (+0.005%) from 97.853% — jayhemnani9910:fix/column-named-like-type-module into agronholm:master

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UUID for postgreSQL

2 participants