python - 使用 Flask SQLAlchemy 连接 3 个表关系
问题描述
我想链接(?不知道是不是一个术语)3个表模型,如图所示:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key = True)
username = db.Column(db.String(15), unique = True, nullable = False)
password = db.Column(db.String(60), nullable = False)
catalogs = db.relationship('UserCatalogs', backref = 'owner', lazy = True)
class UserCatalogs(db.Model):
id = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(20), nullable = False)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False)
child_cat = db.relationship('ChildCatalog', backref = 'parentcatalog', lazy = True)
class ChildCatalog(db.Model):
id = db.Column(db.Integer, primary_key = True)
item_id = db.Column(db.Integer, nullable = True)
catalog_id = db.Column(db.Integer, db.ForeignKey('usercatalogs.id'), nullable = True)
但是当我以这种方式构建表时,稍后在我的应用程序中我无法成功执行这段代码:
item_id = request.values.get('item_id')
catalog_id = request.values.get('catalog_id')
if (item_id is not None and item_id.isnumeric()) and (catalog_id is not None and catalog_id.isnumeric()):
childcatalog = ChildCatalog(item_id=int(item_id), parentcatalog=int(catalog_id))
db.session.add(childcatalog)
db.session.commit()
我得到的错误是“AttributeError:'int'对象没有属性'_sa_instance_state'”,我在Stackoverflow上找到了这个解释,我试图让它与flask_login的'current_user'函数一起工作,但我没有不知道如何使它工作。
这是完整的回溯:
Traceback (most recent call last):
File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 1866, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\ezell\Anaconda3\lib\site-packages\flask\app.py", line 2446, in wsgi_app
return manager.original_init(*mixed[1:], **kwargs)
File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\ext\declarative\base.py", line 842, in _declarative_constructor
setattr(self, k, kwargs[k])
File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 268, in __set__
instance_state(instance), instance_dict(instance), value, None
File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 1003, in set
value = self.fire_replace_event(state, dict_, value, old, initiator)
File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 1026, in fire_replace_event
state, value, previous, initiator or self._replace_token
File "C:\Users\ezell\Anaconda3\lib\site-packages\sqlalchemy\orm\attributes.py", line 1435, in emit_backref_from_scalar_set_event
instance_state(child),
AttributeError: 'int' object has no attribute '_sa_instance_state'
解决这个问题的最佳方法是什么?非常感谢您的时间和关注。
解决方案
推荐阅读
- powershell - Powershell接受来自用户输入的switch语句的多个参数
- php - 我一直在尝试将表单的输入写入文本文件,但它不起作用
- java - 如何为 Web 应用程序配置 Jacoco 代码覆盖率
- python - 在 python selenium 中断言和验证日期格式 DD-MM-YYYY
- android - Android Intents - 如何使用 Intent 共享文本和图像以在 Android 中发送短信
- kubernetes-helm - Helm Chart 限制可以被覆盖的值
- eclipse - Eclipse 在从 Maven 配置文件中获取的类路径中运行带有 MavenDependencies 的 Spring Boot 应用程序
- c# - 如何将程序集版本从构建步骤传递到下一步
- python - 涉及混合整数和字符串的打印命令问题
- c++ - 在 Qt 代码上使用 Fortify SCA(不是过时的“HP Fortify”)