-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
peewee.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (test_peewee_tree.node, CONSTRAINT node_ibfk_1 FOREIGN KEY (parent_id) REFERENCES node (id))') db = MySQLDatabase #737
Comments
Uhh, did you make the |
Yes, parent foreign ey made nullable: mysql_db = MySQLDatabase('test_peewee_tree')
class BaseModel(Model):
class Meta:
database = mysql_db
class Node(BaseModel):
label = CharField()
parent = ForeignKeyField('self', default=0)
left = IntegerField(default=0)
right = IntegerField(default=1)
depth = IntegerField(default=0)
def get_parent(self, level=1):
parent = self
for i in range(level):
parent = parent.parent
return parent
if __name__ == '__main__':
Node.create_table()
n1 = Node(label='root')
n2 = Node(label='a')
n3 = Node(label='b')
n4 = Node(label='c')
n1.append(n2)
n1.append(n3)
n2.append(n4) |
Same peewee.IntegrityError: (1452.... |
No error peewee.IntegrityError: (1452... |
With parent = ForeignKeyField('self', default=None, null=True) works well if peewee version 2.2.4 |
It's very difficult to read your comments as you have not bothered to format the code. I edited one of your comments so you can see how to do it. For what it's worth, 2.2.4 is quite old (about a year and a half). Where is the |
I use hierarchical models in a couple places as well as in the test suite. The right way to do it is parent = ForeignKeyField('self', null=True) If you aren't re-creating your tables every time you change the field then just changing the Python field deifnition won't alter the underlying schema which may have a not-null constraint. |
For peewee version 2.6.4: |
peewee.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (test_peewee_tree.node, CONSTRAINT node_ibfk_1 FOREIGN KEY (parent_id) REFERENCES node (id))')
db = MySQLDatabase
The text was updated successfully, but these errors were encountered: