相关疑难解决方法(0)

Sqlalchemy One - 多一合一

我有两个模型:用户和组。

用户可以在一个组中,因此:

class User(db.Model):
    # other fields
    group_id = db.Column(db.Integer(), db.ForeignKey('group.id'))
Run Code Online (Sandbox Code Playgroud)

但另一方面,我也会有一些有关创建该特定组的用户的信息:

class Group(db.Model):
    # other fields
    users = db.relationship("User", backref='group')
    created_by = db.Column(db.Integer(), db.ForeignKey('user.id'))
Run Code Online (Sandbox Code Playgroud)

结果是:

sqlalchemy.exc.CircularDependencyError: Can't sort tables for DROP; an unresolvable foreign key dependency exists between tables: group, user.  Please ensure that the ForeignKey and ForeignKeyConstraint objects involved in the cycle have names so that they can be dropped using DROP CONSTRAINT.
Run Code Online (Sandbox Code Playgroud)

我试过了use_alter=True,但它给了我:

sqlalchemy.exc.CompileError: Can't emit DROP CONSTRAINT for constraint ForeignKeyConstraint(
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy

5
推荐指数
1
解决办法
1862
查看次数

标签 统计

python ×1

sqlalchemy ×1