How to use sqlalchemy to query in many-to-many relation?
<p>To get a member list in current organization, i used following statement, </p>

<pre><code>from ..members import members
from flask.ext.login import 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()
</code></pre>

<p>but a exception threw out at runtime: AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Group_has_Person.group has an attribute 'organization'</p>

<p>how to get the member list in a right way? the code below is about model definition:</p>

<pre><code>    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")
</code></pre>

<p>table scripts:</p>

<pre><code>CREATE TABLE 'Group' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'title' varchar(45) NOT NULL DEFAULT '' COMMENT 'ç­çº§åç§°',
  'teacher' varchar(45) DEFAULT NULL COMMENT 'è¾å¯¼èå¸',
  'contact' varchar(45) DEFAULT NULL COMMENT 'èç³»æ¹å¼',
  'created_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'åå»ºæ¶é´',
  'status' int(11) NOT NULL COMMENT 'ç¶æï¼-1-å é¤ 1-æ­£å¸¸',
  'Organization_id' int(11) NOT NULL,
  PRIMARY KEY ('id'),
  KEY 'fk_Classes_Organization1_idx' ('Organization_id'),
  CONSTRAINT 'fk_Classes_Organization1' FOREIGN KEY ('Organization_id') REFERENCES 'Organization' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='ç­çº§';

CREATE TABLE 'Person' (
  'id' int(11) NOT NULL AUTO_INCREMENT,
  'User_id' int(11) NOT NULL,
  'nickname' varchar(45) NOT NULL COMMENT '',
  'avatar' varchar(256) NOT NULL COMMENT '',
  'gender' int(11) NOT NULL COMMENT '',
  'birthday' datetime DEFAULT NULL COMMENT '',
  PRIMARY KEY ('id'),
  KEY 'fk_Person_User1_idx' ('User_id'),
  CONSTRAINT 'fk_Person_User1' FOREIGN KEY ('User_id') REFERENCES 'User' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='ä¸ªäºº';

CREATE TABLE 'Group_has_Person' (
  'Group_id' int(11) NOT NULL,
  'Person_id' int(11) NOT NULL,
  'created_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'å å¥æ¶é´',
  'status' int(11) NOT NULL DEFAULT '0' COMMENT 'ç¶æ 0-ç³è¯· 1-æå',
  PRIMARY KEY ('Group_id','Person_id'),
  KEY 'fk_Group_has_Person_Person1_idx' ('Person_id'),
  KEY 'fk_Group_has_Person_Group1_idx' ('Group_id'),
  CONSTRAINT 'fk_Group_has_Person_Group1' FOREIGN KEY ('Group_id') REFERENCES 'Group' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION,
  CONSTRAINT 'fk_Group_has_Person_Person1' FOREIGN KEY ('Person_id') REFERENCES 'Person' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
</code></pre>

<p>I've resolved my question by the following statement:</p>

<pre><code>datas = db_session.query(Group_has_Person).join(Group).filter(Group.organization==current_user.organization).all()
</code></pre>

python sqlalchemy