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
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Version history
===============

**UNRELEASED**

- Fixed ``remote_side`` in a self-referential relationship pointing at the primary key
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)

**4.0.4**

- Added autoincrement to primary key columns to prevent missing field errors.
Expand Down
4 changes: 2 additions & 2 deletions src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -1271,8 +1271,8 @@ def generate_relationships(
# For self referential relationships, remote_side needs to be set
if source is target:
relationship.remote_side = [
source.get_column_attribute(col.name)
for col in constraint.referred_table.primary_key
source.get_column_attribute(element.column.name)
for element in constraint.elements
]

# If the two tables share more than one foreign key constraint,
Expand Down
50 changes: 50 additions & 0 deletions tests/test_generator_declarative.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,56 @@ class SimpleItems(Base):
)


def test_onetomany_selfref_composite_unique_target(generator: CodeGenerator) -> None:
Table(
"simple_items",
generator.metadata,
Column("id", INTEGER, primary_key=True),
Column("scheme", VARCHAR, nullable=False),
Column("code", VARCHAR, nullable=False),
Column("parent_scheme", VARCHAR),
Column("parent_code", VARCHAR),
UniqueConstraint("scheme", "code"),
ForeignKeyConstraint(
["parent_scheme", "parent_code"],
["simple_items.scheme", "simple_items.code"],
),
)

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

from sqlalchemy import ForeignKeyConstraint, Integer, String, UniqueConstraint
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship

class Base(DeclarativeBase):
pass


class SimpleItems(Base):
__tablename__ = 'simple_items'
__table_args__ = (
ForeignKeyConstraint(['parent_scheme', 'parent_code'], \
['simple_items.scheme', 'simple_items.code']),
UniqueConstraint('scheme', 'code')
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
scheme: Mapped[str] = mapped_column(String, nullable=False)
code: Mapped[str] = mapped_column(String, nullable=False)
parent_scheme: Mapped[Optional[str]] = mapped_column(String)
parent_code: Mapped[Optional[str]] = mapped_column(String)

simple_items: Mapped[Optional['SimpleItems']] = relationship('SimpleItems', \
remote_side=[scheme, code], back_populates='simple_items_reverse')
simple_items_reverse: Mapped[list['SimpleItems']] = relationship('SimpleItems', \
remote_side=[parent_scheme, parent_code], back_populates='simple_items')
""",
)


def test_onetomany_composite(generator: CodeGenerator) -> None:
Table(
"simple_items",
Expand Down