当我尝试迁移我的数据库时,我得到CircularDependencyError
。我在Core Exceptions文档中找到了一些有关此内容的信息,并尝试添加post_update=True
,但它没有解决问题。我可以看到这是分支和卡之间相互关系的问题。我该如何解决这个错误?
这些是我的模特:
class Branch(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(255))
Summary = db. Column(db.String(255))
viewCount = db.Column(db.Integer, default=0)
onEditFlag = db.Column(db.Integer)
editorChoice = db.Column(db.Boolean, default=False)
createDateTime = db.Column(db.DateTime, default=db.func.now())
coverImage = db.Column(db.String(255))
userId = db.Column(db.Integer, db.ForeignKey('user.id'))
user = db.relationship('User', backref=db.backref('branches', cascade='all, delete-orphan', lazy='dynamic'))
rootCardId = db.Column(db.Integer, db.ForeignKey('card.id'))
card = db.relationship('Card',
backref=db.backref('childBranches'), post_update=True)
parentBranchId = db.Column(db.Integer, db.ForeignKey('branch.id'))
parentBranch = db.relation('Branch', remote_side=[id])
childeBranch = db.relationship("Branch", lazy="joined", join_depth=2)
parentStreamId = db.Column(db.Integer, db.ForeignKey('stream.id'))
stream = db.relationship('Stream',
backref=db.backref('childBranches', cascade='all, delete-orphan', lazy='dynamic'))
BranchTagRelation = db.relationship('Tag', secondary=BranchTagRelation,
backref=db.backref('branches', lazy='dynamic'))
BranchBookMark = db.relationship('User', secondary=BranchBookMark,
backref=db.backref('branchBookmarks', lazy='dynamic'))
BranchThumbsUp = db.relationship('User', secondary=BranchThumbsUp,
backref=db.backref('branchThumbsUps', lazy='dynamic'))
class Card(db.Model):
id = db.Column(db.Integer, primary_key=True)
branchId = db.Column(db.Integer, db.ForeignKey('branch.id'))
branch = db.relationship('Branch', backref=db.backref('cards', cascade='all, delete-orphan', lazy='dynamic'))
streamId = db.Column(db.Integer, db.ForeignKey('stream.id'))
stream = db.relationship('Stream', backref=db.backref('cards', cascade='all, delete-orphan', lazy='dynamic'))
order = db.Column(db.Float)
content = db.Column(db.Text)
createDateTime = db.Column(db.DateTime, default=db.func.now())
finalEditDateTime = db.Column(db.DateTime)
这是使用这些模型时的例外情况:
Traceback (most recent call last):
File "manager.py", line 6, in <module>
manager.run()
File "C:\Python27\lib\site-packages\flask_script\__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "C:\Python27\lib\site-packages\flask_script\__init__.py", line 383, in handle
res = handle(*args, **config)
File "C:\Python27\lib\site-packages\flask_script\commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "C:\Python27\lib\site-packages\flask_migrate\__init__.py", line 80, in migrate
command.revision(config, message, autogenerate = True, sql = sql)
File "C:\Python27\lib\site-packages\alembic\command.py", line 97, in revision
script.run_env()
File "C:\Python27\lib\site-packages\alembic\script.py", line 203, in run_env
util.load_python_file(self.dir, 'env.py')
File "C:\Python27\lib\site-packages\alembic\util.py", line 212, in load_python_file
module = load_module_py(module_id, path)
File "C:\Python27\lib\site-packages\alembic\compat.py", line 58, in load_module_py
mod = imp.load_source(module_id, path, fp)
File "migrations\env.py", line 72, in <module>
run_migrations_online()
File "migrations\env.py", line 65, in run_migrations_online
context.run_migrations()
File "<string>", line 7, in run_migrations
File "C:\Python27\lib\site-packages\alembic\environment.py", line 688, in run_migrations
self.get_context().run_migrations(**kw)
File "C:\Python27\lib\site-packages\alembic\migration.py", line 242, in run_migrations
self):
File "C:\Python27\lib\site-packages\alembic\command.py", line 83, in retrieve_migrations
autogen._produce_migration_diffs(context, template_args, imports)
File "C:\Python27\lib\site-packages\alembic\autogenerate\api.py", line 137, in _produce_migration_diffs
autogen_context, object_filters, include_schemas)
File "C:\Python27\lib\site-packages\alembic\autogenerate\api.py", line 209, in _produce_net_changes
for table in metadata.sorted_tables])
File "C:\Python27\lib\site-packages\sqlalchemy\sql\schema.py", line 3194, in sorted_tables
return ddl.sort_tables(self.tables.values())
File "C:\Python27\lib\site-packages\sqlalchemy\sql\ddl.py", line 868, in sort_tables
return list(topological.sort(tuples, tables))
File "C:\Python27\lib\site-packages\sqlalchemy\util\topological.py", line 47, in sort
for set_ in sort_as_subsets(tuples, allitems):
File "C:\Python27\lib\site-packages\sqlalchemy\util\topological.py", line 34, in sort_as_subsets
_gen_edges(edges)
CircularDependencyError: Circular dependency detected. Cycles: set([Table('card', MetaData(bind=None), Column('id', Integer(), table=<card>, primary_key=True, nullable=False), Column('branchId', Integer(), ForeignKey
('branch.id'), table=<card>), Column('streamId', Integer(), ForeignKey('stream.id'), table=<card>), Column('order', Float(), table=<card>), Column('content', Text(), table=<card>), Column('createDateTime', DateTime()
, table=<card>, default=ColumnDefault(<sqlalchemy.sql.functions.now at 0x3054650; now>)), Column('finalEditDateTime', DateTime(), table=<card>), schema=None), Table('branch', MetaData(bind=None), Column('id', Integer
(), table=<branch>, primary_key=True, nullable=False), Column('title', String(length=255), table=<branch>), Column('Summary', String(length=255), table=<branch>), Column('viewCount', Integer(), table=<branch>, defaul
t=ColumnDefault(0)), Column('onEditFlag', Integer(), table=<branch>), Column('editorChoice', Boolean(), table=<branch>, default=ColumnDefault(False)), Column('createDateTime', DateTime(), table=<branch>, default=Colu
mnDefault(<sqlalchemy.sql.functions.now at 0x3041930; now>)), Column('coverImage', String(length=255), table=<branch>), Column('userId', Integer(), ForeignKey('user.id'), table=<branch>), Column('rootCardId', Integer
(), ForeignKey('card.id'), table=<branch>), Column('parentBranchId', Integer(), ForeignKey('branch.id'), table=<branch>), Column('parentStreamId', Integer(), ForeignKey('stream.id'), table=<branch>), schema=None)]) a
ll edges: set([(Table('card', MetaData(bind=None), Column('id', Integer(), table=<card>, primary_key=True, nullable=False), Column('branchId', Integer(), ForeignKey('branch.id'), table=<card>), Column('streamId', Int
eger(), ForeignKey('stream.id'), table=<card>), Column('order', Float(), table=<card>), Column('content', Text(), table=<card>), Column('createDateTime', DateTime(), table=<card>, default=ColumnDefault(<sqlalchemy.sq
l.functions.now at 0x3054650; now>)), Column('finalEditDateTime', DateTime(), table=<card>), schema=None), Table('StreamBookMark', MetaData(bind=None), Column('streamId', Integer(), ForeignKey('stream.id'), table=<St
reamBookMark>), Column('userId', Integer(), ForeignKey('user.id'), table=<StreamBookMark>), Column('cardId', Integer(), ForeignKey('card.id'), table=<StreamBookMark>), Column('createDateTime', DateTime(), table=<Stre
amBookMark>, default=ColumnDefault(<sqlalchemy.sql.functions.now at 0x2ff1c70; now>)), schema=None)),