我有两个相互引用的 SQLAlchemy 类声明,所以第一个给出错误,因为第二个尚未声明。
Keywith中的记录key_type_id == 4是从一个Entity人Entity通过parent_entity_id.
要定义Entity收集所有子实体的关系,我需要添加一个反向引用,但该引用key稍后声明。
class Entity(db.Model):
__tablename__ = 'entity'
entity_id = db.Column(db.INTEGER, primary_key=True)
...
children = db.relationship(
'Entity', secondary=key,
primaryjoin="and_(key.c.entity_id == entity_id, "
"key.c.key_type_id == 4)",
secondaryjoin=(key.c.parent_entity_id == entity_id),
backref=db.backref('key', lazy='dynamic'), lazy='dynamic')
class Key(db.Model):
__tablename__ = 'key'
...
entity_id = db.Column(db.ForeignKey('entity.entity_id'), nullable=False,
index=True)
...
key_type_id = db.Column(db.ForeignKey('key_type.key_type_id'), index=True)
# 4 for a foreign key
...
parent_entity_id = db.Column(db.INTEGER, index=True)
...
错误回溯是。
ipdb> Traceback (most recent call last):
File "<ipython-input-1-a3063c2d9856>", line 1, in <module>
debugfile('C:/Users/Mark Kortink/Dropbox/Python/projects/metapplica/_dev/Scraps/ooClass2DBs.py', wdir='C:/Users/Mark Kortink/Dropbox/Python/projects/metapplica/_dev/Scraps')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 856, in debugfile
debugger.run("runfile(%r, args=%r, wdir=%r)" % (filename, args, wdir))
File "C:\ProgramData\Anaconda3\lib\bdb.py", line 585, in run
exec(cmd, globals, locals)
File "<string>", line 1, in <module>
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile
execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
首先,我是否正确声明了 backref 关系?二、如何打破僵局?
温温酱
相关分类