flask 一对多 自引用 关系怎么写?

我想要写一个系统评论,一条评论下面又会又多条回复,所以是一个一对多的自引用,我的写法如下:

class Comment(db.Model):
  __tablename__ = 'comments'
  id = db.Column(db.Integer, primary_key = True)
  body = db.Column(db.Text)
  author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
  comments = db.relationship('Comment', backref="response", remote_side = 'Comment.id')
  response_id = db.Column(db.Integer, db.ForeignKey('comments.id'))
  timestamp = db.Column(db.DateTime, index = True, default = datetime.utcnow)
  

添加评论正常,但是添加评论回复的时候报错:

Traceback (most recent call last):
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask_cors/extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/lushg/awesome/sites-flask/app/api/decorators.py", line 21, in decorated_function
    return f(*args, **kwargs)
  File "/home/lushg/awesome/sites-flask/app/api/posts.py", line 215, in add_comment
    comment = Comment(**params)
  File "<string>", line 4, in __init__
    
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/sqlalchemy/orm/state.py", line 424, in _initialize_instance
    manager.dispatch.init_failure(self, args, kwargs)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/sqlalchemy/util/compat.py", line 249, in reraise
    raise value
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/sqlalchemy/orm/state.py", line 421, in _initialize_instance
    return manager.original_init(*mixed[1:], **kwargs)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/sqlalchemy/ext/declarative/base.py", line 747, in _declarative_constructor
    setattr(self, k, kwargs[k])
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/sqlalchemy/orm/attributes.py", line 229, in __set__
    instance_dict(instance), value, None)
  File "/home/lushg/awesome/sites-flask/venv/lib/python3.5/site-packages/sqlalchemy/orm/attributes.py", line 1042, in set
    given, wanted))
TypeError: Incompatible collection type: Comment is not list-like

写法有什么问题吗?(因为一对多不想用第三张表来实现。)

红颜莎娜
浏览 752回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python