sqlalchemy地图列出相关对象

发布于 2025-02-12 08:11:25 字数 1942 浏览 1 评论 0原文

我在sqlalchemy中两个实体之间的映射关系时有问题。我正在尝试实现下一个行为 - >向用户映射所有相关的反馈。该关系是另一侧的receiver_id,等于user_id。因此,用户1正在向用户2发送反馈,当我获得用户2对象时,我正在尝试将其作为列表。我有下一个代码,

class User(Base):
    __tablename__ = "user"
    user_id = sa.Column('user_id', UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    first_name = sa.Column('first_name', sa.String)
    last_name = sa.Column('last_name', sa.String)
    phone_number = sa.Column('phone_number', sa.String(20), unique=True)
    email = sa.Column('email', sa.String, unique=True)
    is_active = sa.Column('is_active', sa.Boolean, server_default=expression.true(), nullable=False)
    deleted = sa.Column('deleted', sa.Boolean, server_default=expression.false(), nullable=False)
    created_time = sa.Column('created_time', sa.DateTime, server_default=func.now(), nullable=False)
    updated_time = sa.Column('updated_time', sa.DateTime, server_default=func.now(), nullable=False)
    deleted_time = sa.Column('deleted_time', sa.DateTime)
    feedbacks = relationship('UserFeedback', primaryjoin='User.user_id == UserFeedback.receiver_id', uselist=True, lazy='dynamic', backref='user_feedback')


class UserFeedback(Base):
    __tablename__ = "user_feedback"
    id = sa.Column('id', sa.Integer, primary_key=True, autoincrement=True)
    sender_id = sa.Column(UUID(as_uuid=True), ForeignKey("user.user_id"), nullable=False)
    receiver_id = sa.Column(UUID(as_uuid=True), ForeignKey("user.user_id"), nullable=False)

    sender = relationship("User", foreign_keys=[sender_id])
    receiver = relationship("User", foreign_keys=[receiver_id])

    rating = sa.Column('rating', sa.DECIMAL, nullable=False)
    feedback = sa.Column('feedback', sa.String, nullable=False)
    date = sa.Column('date', sa.DateTime, server_default=func.now(), nullable=False)

我已经填写了数据库中的用户反馈,但是当我提取用户ORM时,没有绘制相关的反馈。实际上,它甚至没有这个领域,所以

我正在学习Sqlalchemy,这有点令人困惑,因此会很高兴

I have a problem with mapping relation between two entities in SQLAlchemy. I am trying to achieve the next behaviour -> map to User all related Feedbacks. The relation is by receiver_id on the other side, which is equal to user_id. So, User 1 is sending a feedback for User 2 and I am trying to get them as a list when I get User 2 object. I have next code

class User(Base):
    __tablename__ = "user"
    user_id = sa.Column('user_id', UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
    first_name = sa.Column('first_name', sa.String)
    last_name = sa.Column('last_name', sa.String)
    phone_number = sa.Column('phone_number', sa.String(20), unique=True)
    email = sa.Column('email', sa.String, unique=True)
    is_active = sa.Column('is_active', sa.Boolean, server_default=expression.true(), nullable=False)
    deleted = sa.Column('deleted', sa.Boolean, server_default=expression.false(), nullable=False)
    created_time = sa.Column('created_time', sa.DateTime, server_default=func.now(), nullable=False)
    updated_time = sa.Column('updated_time', sa.DateTime, server_default=func.now(), nullable=False)
    deleted_time = sa.Column('deleted_time', sa.DateTime)
    feedbacks = relationship('UserFeedback', primaryjoin='User.user_id == UserFeedback.receiver_id', uselist=True, lazy='dynamic', backref='user_feedback')


class UserFeedback(Base):
    __tablename__ = "user_feedback"
    id = sa.Column('id', sa.Integer, primary_key=True, autoincrement=True)
    sender_id = sa.Column(UUID(as_uuid=True), ForeignKey("user.user_id"), nullable=False)
    receiver_id = sa.Column(UUID(as_uuid=True), ForeignKey("user.user_id"), nullable=False)

    sender = relationship("User", foreign_keys=[sender_id])
    receiver = relationship("User", foreign_keys=[receiver_id])

    rating = sa.Column('rating', sa.DECIMAL, nullable=False)
    feedback = sa.Column('feedback', sa.String, nullable=False)
    date = sa.Column('date', sa.DateTime, server_default=func.now(), nullable=False)

I've filled the database with user feedbacks but when I pull user ORM doesn't map related feedbacks. It actually doesn't even have this field, so that's a little confusing

I am learning SQLAlchemy, so would appreciate help or recommendation on that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌生 2025-02-19 08:11:25

问题是您正在使用动态与关系的加载策略(文档在这里)。

使用懒='Dynamic'查询时,SQLalchemy会生成单独的查询,而不是获取实际结果。因此,在您的初始情况下,反馈属性包含另一个选择语句(appenderquery类型)您可以手动执行。

为了简单地获取相关数据,只需将“动态”更改为“加入”。

您可以在文档或in <一个href =“ https://medium.com/@ns2586/sqlalchemys-relationshiphiphip-and-lazy-parameter-4a5553257d9ef” rel =“ nofollow noreferrer”>这个美丽的帖子。

The thing is that you're using dynamic loading strategy for relationship (the docs are here).

When querying with lazy = 'dynamic', SQLAlchemy generates a separate query rather than fetching actual results. So in your initial case feedbacks attribute contains another select statement (of AppenderQuery type) you can execute manually.

To simply get the related data just change 'dynamic' to 'joined'.

You can read more about relationship loading strategies in the docs or in this beautiful post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文