在sqlalchemy中如何进行many-to-many的查询?

新手上路,请多包涵

我希望获取当前机构下的成员列表,下面是我的查询方式:

from ..members import members
from flask.ext.login import login_required, current_user
from app.common.database import db_session

@members.route("/", methods=["GET"])
@login_required
def index():
    datas = db_session.query(Group_has_Person).filter(Group_has_Person.group.organization==current_user.organization).all()
    

在实际运行时会报一下错误:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Group_has_Person.group has an attribute 'organization'

请问该如何解决,以下是数据模型的定义:

    class Organization(Base):
        """机构"""
        __tablename__ = 'Organization'
        id = Column(Integer, primary_key = True)
        title = Column(String(45), nullable = False)
        logo = Column(String(256), nullable = False)
        contact_name = Column(String(45), nullable = False)
        contact_phone = Column(String(45), nullable = False)
        created_time = Column(DateTime, nullable = False, default = datetime.now())
        User_id = Column(Integer, ForeignKey('User.id'))
        user = relationship("User", back_populates="organization")
        groups = relationship("Group", back_populates="organization")

    class Group(Base):
        """班级"""
        __tablename__ = 'Group'
        id = Column(Integer, primary_key = True)
        title = Column(String(45), nullable = False)
        teacher = Column(String(45), nullable = True)
        contact = Column(String(45), nullable = True)
        created_time = Column(DateTime, nullable = False, default = datetime.now())
        status = Column(Integer, nullable = False, default = 1)
        Organization_id = Column(Integer, ForeignKey('Organization.id'))
        organization = relationship("Organization", back_populates="groups")
        members = relationship("Group_has_Person", back_populates="group")

    class Person(Base):
        """班级"""
        __tablename__ = 'Person'
        id = Column(Integer, primary_key = True)
        nickname = Column(String(45), nullable = False)
        avatar = Column(String(256), nullable = False)
        gender = Column(Integer, nullable = False)
        birthday = Column(DateTime, nullable = False)
        User_id = Column(Integer, ForeignKey('User.id'))
        user = relationship("User", back_populates="person")
        groups = relationship("Group_has_Person", back_populates="person")

    class Group_has_Person(Base):
        """班级成员"""
        __tablename__ = 'Group_has_Person'
        Group_id = Column(Integer, ForeignKey('Group.id'), primary_key = True)
        Person_id = Column(Integer, ForeignKey('Person.id'), primary_key = True)
        created_time = Column(DateTime, nullable = False, default = datetime.now())
        status = Column(Integer, nullable = False, default = 0)
        group = relationship("Group", back_populates="members")
        person = relationship("Person", back_populates="groups")
阅读 2.8k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进