gpt4 book ai didi

python - SQLAlchemy 对删除的循环依赖

转载 作者:太空宇宙 更新时间:2023-11-03 16:59:02 25 4
gpt4 key购买 nike

我在SQLAlchemy 1.0.11中构建了一些如下所示的模型

class HasID(object):
@declared_attr
def id(cls):
return Column('id', Integer, Sequence('test_id_seq'), primary_key=True)
...


class TestParent(HasID, Model)
__tablename__ = 'tests'
discriminator = Column(String(50))
__mapper_args__ = {'polymorphic_on': discriminator}
...


class FooTest(TestParent, Model):
__tablename__ = 'footests'
__mapper_args__ = {'polymorphic_identity': 'footests'}
id = Column(Integer, ForeignKey('tests.id'), primary_key=True)
parent_id = Column(Integer, ForeignKey('footests.id'))
children = relationship('FooTest',
foreign_keys='FooTest.id',
lazy='joined',
join_depth=2,
cascade='save-update, merge, delete, delete-orphan')
...


class BarTest(TestParent, Model):
__tablename__ = 'bartests'
__mapper_args__ = {'polymorphic_identity': 'bartests'}
id = Column(Integer, ForeignKey('tests.id'), primary_key=True)
...

现在,当我尝试使用以下方法删除 BarTest 实例时:

mytest = TestParent.query.get(30) # is an instance of BarTest
db_session.delete(mytest)
db_session.commit()

我得到以下回溯:

Connected to pydev debugger (build 143.1559)
Traceback (most recent call last):
File "/home/xxx/Downloads/pycharm-5.0.3/helpers/pydev/pydevd.py", line 2407, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/home/xxx/Downloads/pycharm-5.0.3/helpers/pydev/pydevd.py", line 1798, in run
launch(file, globals, locals) # execute the script
File "/home/xxx/xxx/data_app/scratches/sql_scratcher.py", line 38, in <module>
db_session.commit()
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 150, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 813, in commit
self.transaction.commit()
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 392, in commit
self._prepare_impl()
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl
self.session.flush()
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2027, in flush
self._flush(objects)
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2145, in _flush
transaction.rollback(_capture_exception=True)
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2109, in _flush
flush_context.execute()
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 365, in execute
postsort_actions):
File "/home/xxx/xxx/data_app/env/lib/python2.7/site-packages/sqlalchemy/util/topological.py", line 36, in sort_as_subsets
_gen_edges(edges)
sqlalchemy.exc.CircularDependencyError: Circular dependency detected. (DeleteState(<FooTest at 0x7f9b74a2e590>), DeleteState(<FooTest at 0x7f9b74a2e510>))

Process finished with exit code 1

我对如何设置 FooTest 感到困惑。我试图通过与 TestParent 交互来删除 BarTest 的实例,尽管收到了引用 FooTest

的错误

最佳答案

SQLAlchemy 邮件列表之王提供的答案是我需要改变

class FooTest(TestParent, Model):
__tablename__ = 'footests'
__mapper_args__ = {'polymorphic_identity': 'footests'}
id = Column(Integer, ForeignKey('tests.id'), primary_key=True)
parent_id = Column(Integer, ForeignKey('footests.id'))
children = relationship('FooTest',
foreign_keys='FooTest.id',
lazy='joined',
join_depth=2,
cascade='save-update, merge, delete, delete-orphan')

class FooTest(TestParent):
__tablename__ = 'footests'
__mapper_args__ = {'polymorphic_identity': 'footests'}
id = Column(Integer, ForeignKey('tests.id'), primary_key=True)
parent_id = Column(Integer, ForeignKey('footests.id'))
children = relationship('FooTest',
foreign_keys='FooTest.parent_id', # CHANGE HERE!!
lazy='joined',
join_depth=2,
cascade='save-update, merge, delete, delete-orphan')

我在 FooTest 父级和 FooTest 子级之间的关系上指定了不正确的外键。

我还从 FooTest 继承中删除了 Model(根据 Boris 的评论),尽管这并没有导致任何错误

关于python - SQLAlchemy 对删除的循环依赖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35163325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com