多对多关系配置遇到问题 - “sqlalchemy.exc.InvalidRequestError”[已解决]

问题描述 投票:0回答:1

我是 SQLAlchemy 新手,在 ManyToMany 关系配置方面遇到一些错误。

在将代码粘贴到此处之前,我想给出一个简短的描述:我有一个模型

UserORM
ProjectORM
,它们是多对多相关的。另外,我创建了一个关联表
UserProjectORM
,该表还有一些附加字段。

所以,这是

ProjectORM

class ProjectORM(AbstractModel):
    __tablename__ = "projects"

    status: Mapped[EnumStatus]
    title: Mapped[str] = mapped_column(String(255), unique=True)
    description: Mapped[str | None]

    category_id: Mapped[int] = mapped_column(ForeignKey("categories.id"))
    category: Mapped[CategoryORM] = relationship(back_populates="projects")

    users: Mapped[list["UserProjectORM"]] = relationship(back_populates="projects")
    resources: Mapped[list["ResourceORM"]] = relationship(back_populates="project")

    time_created: Mapped[time_created]

这是

UserORM
放置在另一个文件中:

class UserORM(AbstractModel):
    __tablename__ = "users"

    username: Mapped[str] = mapped_column(String(35), unique=True)
    age: Mapped[str] = mapped_column(String(3), nullable=True)
    email: Mapped[str] = mapped_column(String(155), unique=True)

    surname: Mapped[str] = mapped_column(String(100))
    firstname: Mapped[str] = mapped_column(String(100))
    patronymic: Mapped[str] = mapped_column(String(100), nullable=True)
    about: Mapped[str] = mapped_column(nullable=True)
    file_path: Mapped[str] = mapped_column(nullable=True)

    hashed_password: Mapped[str] = mapped_column(String(125))
    is_active: Mapped[bool] = mapped_column(default=False)
    time_created: Mapped[time_created]

    tokens: Mapped[list["TokenORM"]] = relationship(back_populates="user")
    projects: Mapped[list["project.UserProjectORM"]] = relationship(back_populates="users")

和关联表

UserProjectORM
以及一些额外的字段:

class UserProjectORM(AbstractModel):
    __tablename__ = "users_projects"

    user_id: Mapped[int] = mapped_column(ForeignKey("users.id"), primary_key=True)
    project_id: Mapped[int] = mapped_column(ForeignKey("projects.id"), primary_key=True)
    user: Mapped["user.UserORM"] = relationship(back_populates="projects")
    project: Mapped["ProjectORM"] = relationship(back_populates="users")

    is_author: Mapped[bool] = mapped_column(Boolean, default=False)
    datatime_joined: Mapped[time_created]

time_created
字段是从另一个文件导入的:

time_created = Annotated[
    datetime.datetime,
    mapped_column(DateTime(timezone=True), server_default=func.now()),
]

这是我的错误:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper[ProjectORM(projects)]'. Original exception was: Mapper 'Mapper[UserProjectORM(users_projects)]' has no property 'projects'.  If this property was indicated from other mappers or configure events, ensure registry.configure() has been called.


/usr/local/lib/python3.11/site-packages/sqlalchemy/orm/mapper.py:4240: InvalidRequestError
python postgresql sqlalchemy asyncpg
1个回答
0
投票

答案似乎很简单。

问题出在这些方面:

projects: Mapped[list["project.UserProjectORM"]] = relationship(back_populates="users")
users: Mapped[list["UserProjectORM"]] = relationship(back_populates="projects")

所以我把它改为:

projects: Mapped[list["project.UserProjectORM"]] = relationship(back_populates="user")
users: Mapped[list["UserProjectORM"]] = relationship(back_populates="project")

原因是

SQLAlchemy
试图找到
projects
不存在的
users
/
Mapper
属性。然而,正确的属性是
project
user

© www.soinside.com 2019 - 2024. All rights reserved.