Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 用炼金术连接3个表关系_Python_Flask_Database Design_Sqlalchemy - Fatal编程技术网

Python 用炼金术连接3个表关系

Python 用炼金术连接3个表关系,python,flask,database-design,sqlalchemy,Python,Flask,Database Design,Sqlalchemy,我想连锁连接(?不知道这是否是一个术语)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), nullab

我想连锁连接(?不知道这是否是一个术语)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'对象没有属性''u 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'

解决这个问题的最好办法是什么?非常感谢您的时间和关注。

请展示完整的回溯。我无法将错误与您显示的代码匹配。我已添加完整的回溯,对于给您带来的不便,深表歉意!你有两个问题。首先,
ChildCatalogs
将创建一个名为
ChildCatalogs
的表,并带有下划线。其次,您试图传递一个命名参数,该参数是反向引用
childcatalog=childcatalog(item\u id=int(item\u id),catalog\u id=int(catalog\u id))
非常感谢您解决了我的问题!但是我的catalog\u id必须以某种方式传入,以便我可以使用request.values.get捕获的特定catalog\u id保存该项。我不知道如何实现这一点。我向您演示了如何传递
目录id