It's weird to me that the following code always gets into a "maximum recursion 
depth" exception. What I am trying to do here is that the comment model has two 
foreign keys referring to two other models each of which should be able to back 
reference comments. There must be something wrong in the model definition. 
Thanks for pointing it out.


class Comment(db.Model):
    __tablename__ = 'comments'

    id = db.Column(db.Integer, db.Sequence('comment_id_seq'), primary_key=True)
    time_created = db.Column(db.DateTime, default=db.func.now())
    body = db.Column(db.UnicodeText, nullable=False)
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'), index=True)
    story_id = db.Column(db.Integer, db.ForeignKey('stories.id'), index=True)

    author = db.relationship("User", backref=db.backref('comments', 
order_by=time_created))
    story = db.relationship("Story", backref=db.backref('comments', 
order_by=time_created))

    def __init__(self, body, author, story):
        self.body = body
        self.author = author
        self.story = story

def create_new_comment(author_id, story, body):
    author = User.query.get(author_id)
    comment = Comment(author, story, body)

    try:
        db.session.add(comment)
        db.session.commit()
        return comment
    except SQLAlchemyError:
        logger.info("problem when creating comment")
        db.session.rollback()
        return None

story = Story.query.get(1)
create_new_comment(1, story, 'test comment')

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/sqlalchemy/-/c1IslgIkhIIJ.
To post to this group, send email to sqlalchemy@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.

Reply via email to