Skip to content

Commit ef78f36

Browse files
committed
Added workaround for missing python_type in postgres UUID column
Fixes #184.
1 parent c07aa67 commit ef78f36

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Version history
55

66
- Migrated all packaging/testing configuration to ``pyproject.toml``
77
. Fixed ``KeyError`` when rendering an index without any columns
8+
- Worked around PostgreSQL UUID columns getting ``Any`` as the type annotation
89

910
**3.0.0b3**
1011

src/sqlacodegen/generators.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,9 @@ def collect_imports_for_column(self, column: Column[Any]) -> None:
12311231
try:
12321232
python_type = column.type.python_type
12331233
except NotImplementedError:
1234-
self.add_literal_import("typing", "Any")
1234+
if column.type.__class__.__name__ != "UUID":
1235+
# Workaround for https://github.com/sqlalchemy/sqlalchemy/issues/7943
1236+
self.add_literal_import("typing", "Any")
12351237
else:
12361238
self.add_import(python_type)
12371239

@@ -1266,7 +1268,11 @@ def render_column_attribute(self, column_attr: ColumnAttribute) -> str:
12661268
try:
12671269
python_type = column.type.python_type
12681270
except NotImplementedError:
1269-
python_type_name = "Any"
1271+
if column.type.__class__.__name__ == "UUID":
1272+
# Workaround for https://github.com/sqlalchemy/sqlalchemy/issues/7943
1273+
python_type_name = "str"
1274+
else:
1275+
python_type_name = "Any"
12701276
else:
12711277
python_type_name = python_type.__name__
12721278

tests/test_generators.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from _pytest.fixtures import FixtureRequest
77
from sqlalchemy import PrimaryKeyConstraint
88
from sqlalchemy.dialects import mysql, postgresql
9+
from sqlalchemy.dialects.postgresql import UUID
910
from sqlalchemy.engine import Engine, create_engine
1011
from sqlalchemy.schema import (
1112
CheckConstraint,
@@ -2393,3 +2394,34 @@ class SimpleItems:
23932394
container: Optional[SimpleContainers] = field(default=None, metadata={'sa': relationship('SimpleContainers', back_populates='simple_items')})
23942395
""",
23952396
)
2397+
2398+
def test_uuid_type_annotation(self, generator: CodeGenerator) -> None:
2399+
Table(
2400+
"simple",
2401+
generator.metadata,
2402+
Column("id", UUID, primary_key=True),
2403+
)
2404+
2405+
validate_code(
2406+
generator.generate(),
2407+
"""\
2408+
from __future__ import annotations
2409+
2410+
from dataclasses import dataclass, field
2411+
2412+
from sqlalchemy import Column
2413+
from sqlalchemy.dialects.postgresql import UUID
2414+
from sqlalchemy.orm import registry
2415+
2416+
mapper_registry = registry()
2417+
2418+
2419+
@mapper_registry.mapped
2420+
@dataclass
2421+
class Simple:
2422+
__tablename__ = 'simple'
2423+
__sa_dataclass_metadata_key__ = 'sa'
2424+
2425+
id: str = field(init=False, metadata={'sa': Column(UUID, primary_key=True)})
2426+
""",
2427+
)

0 commit comments

Comments
 (0)