On Aug 23, 2010, at 5:37 AM, Freewind wrote: > I'm new to python(sqlalchemy), and I'm learning to build web site with > pylons and sqlalchemy. > > I have a problem when I declare the relationship between models. I've > tried it several hours, but failed. But I think it should be a basic > question. > > I have two classes: User and Article, user can create articles, and > modified the other people's article(like wiki). > So a user has created-articles and edited-articles. > > > class Article(Base): > __tablename__ = 'articles' > > id = Column(Integer, primary_key=True) > title = ... > > user_id = Column(Integer, ForeignKey('users.id')) > editor_id = Column(Integer, ForeignKey('users.id')) > > # relations > user = relationship('User', backref='articles') # -> has error > > > class User(Base): > pass > > > But there is an error displayed: > InvalidRequestError: One or more mappers failed to compile. Exception > was probably suppressed within a hasattr() call. Message was: Could > not determine join condition between parent/child tables on > relationship Article.user. Specify a 'primaryjoin' expression. If this > is a many-to-many relationship, 'secondaryjoin' is needed as well. > > I tried to add `primaryjoin` to the line('has error'), but don't know > what it should be. I tried some codes, but none works.
you want to say this: class Article(Base): __tablename__ = 'articles' id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey('users.id')) editor_id = Column(Integer, ForeignKey('users.id')) user = relationship('User', backref='articles', primaryjoin="Article.user_id==User.id") class User(Base): __tablename__ = 'users' id = Column(Integer, primary_key=True) this to resolve the ambiguity of how Article would join to User, given that there are two different foreign keys that reference the 'users' table. Background documentation is at: http://www.sqlalchemy.org/docs/mappers.html?highlight=primaryjoin#specifying-alternate-join-conditions-to-relationship the quoting is specific to declarative: http://www.sqlalchemy.org/docs/reference/ext/declarative.html?highlight=primaryjoin#configuring-relationships > > Thank you in advance! > > -- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To post to this group, send email to sqlalch...@googlegroups.com. > To unsubscribe from this group, send email to > sqlalchemy+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/sqlalchemy?hl=en. > -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.