Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ Version history
when the foreign key targets other columns (such as a ``UNIQUE`` constraint), which
made mapper configuration fail with ``ArgumentError``
(`#484 <https://github.com/agronholm/sqlacodegen/issues/484>`_; PR by @NixBiks)
- 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

**4.0.4**

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
]
requires-python = ">=3.10"
dependencies = [
"SQLAlchemy >= 2.0.29",
"SQLAlchemy >= 2.0.29, < 2.1",
"inflect >= 4.0.0",
]
dynamic = ["version"]
Expand Down
2 changes: 1 addition & 1 deletion src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ def find_free_name(
name = _re_invalid_identifier.sub("_", name)
if name[0].isdigit():
name = "_" + name
elif iskeyword(name) or name == "metadata":
elif iskeyword(name) or name in ("metadata", "registry"):
name += "_"

original = name
Expand Down
29 changes: 29 additions & 0 deletions tests/test_generator_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -2129,6 +2129,35 @@ class Simple(Base):
)


def test_registry_column(generator: CodeGenerator) -> None:
Table(
"simple",
generator.metadata,
Column("id", INTEGER, primary_key=True),
Column("registry", VARCHAR),
)

validate_code(
generator.generate(),
"""\
from typing import Optional

from sqlalchemy import Integer, String
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
pass


class Simple(Base):
__tablename__ = 'simple'

id: Mapped[int] = mapped_column(Integer, primary_key=True)
registry_: Mapped[Optional[str]] = mapped_column('registry', String)
""",
)


def test_metadata_column(generator: CodeGenerator) -> None:
Table(
"simple",
Expand Down
Loading