Skip to content

Mapper cannot access relationship direction attribute #243

@david-1792

Description

@david-1792

I am running the mapper using async SQLAlchemy with a simple one-to-many relationship like the following.

# app.api.teams.models
class Team(Base):
    __tablename__ = 'team'

    id: Mapped[uuid.UUID] = mapped_column(types.UUID, primary_key=True, default=uuid.uuid4)

    name: Mapped[str]
    headquarters: Mapped[str]

# app.api.teams.graphql.types
@graphql_mapper.type(m.Team)
class Team:
    pass

# app.api.heroes.models
class Hero(Base):
    __tablename__ = 'hero'

    id: Mapped[uuid.UUID] = mapped_column(types.UUID, primary_key=True, default=uuid.uuid4)

    name: Mapped[str]
    secret_name: Mapped[str]
    age: Mapped[int]

    team_id: Mapped[uuid.UUID] = mapped_column(types.UUID, ForeignKey('team.id'))
    team: Mapped["Team"] = relationship()

# app.api.heroes.graphql.types
@graphql_mapper.type(m.Hero)
class Hero:
    __exclude__ = ['secret_name']

This example works fine and I can access the Team type through the Hero type. The problem arises when I create the heroes attribute on the Team class (to make the relationship bi-lateral)

# app.api.teams.models
def resolve_hero():
    from app.api.heroes.models import Hero
    return Hero

class Team(Base):
    __tablename__ = 'team'

    id: Mapped[uuid.UUID] = mapped_column(types.UUID, primary_key=True, default=uuid.uuid4)

    name: Mapped[str]
    headquarters: Mapped[str]

    heroes: Mapped[List["Hero"]] = relationship(resolve_hero, back_populates='team')

# app.api.heroes.models
class Hero(Base):
    __tablename__ = 'hero'

    id: Mapped[uuid.UUID] = mapped_column(types.UUID, primary_key=True, default=uuid.uuid4)

    name: Mapped[str]
    secret_name: Mapped[str]
    age: Mapped[int]

    team_id: Mapped[uuid.UUID] = mapped_column(types.UUID, ForeignKey('team.id'))
    team: Mapped["Team"] = relationship("Team", back_populates='heroes')

At this moment, the app does not deploy and I get the following error message.

  File "/app/app/api/heroes/graphql/types.py", line 13, in <module>
    @graphql_mapper.type(m.Hero)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/strawberry_sqlalchemy_mapper/mapper.py", line 699, in convert
    strawberry_type = self._convert_relationship_to_strawberry_type(
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/strawberry_sqlalchemy_mapper/mapper.py", line 397, in _convert_relationship_to_strawberry_type
    if self._get_relationship_is_optional(relationship):
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/strawberry_sqlalchemy_mapper/mapper.py", line 406, in _get_relationship_is_optional
    if relationship.direction in [ONETOMANY, MANYTOMANY]:
       ^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 1332, in __getattr__
    return self._fallback_getattr(key)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/app/.venv/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 1301, in _fallback_getattr
    raise AttributeError(key)
AttributeError: direction

I am using strawberry v0.266.0 and strawberry-sqlalchemy-mapper v.0.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    awaiting author responseAwaiting response from issue openerbugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions