【发布时间】:2011-07-09 14:40:17
【问题描述】:
尝试将记录插入到名为 Foods 的表中时,我收到来自 web2py 的 IntegrityError。该表有一个指向 Recipes 的外键,但我想添加一个没有 recipe_id 的行。
<class 'gluon.contrib.pymysql.err.IntegrityError'>((1452, u'Cannot add or update a child row: a foreign key constraint fails (`pymeals`.`foods`, CONSTRAINT `foods_ibfk_1` FOREIGN KEY (`recipe_id`) REFERENCES `recipes` (`id`) ON DELETE CASCADE)'))
这是我的桌子
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| recipe_id | int(11) | YES | MUL | NULL | |
+-----------+--------------+------+-----+---------+----------------+
我已经使用以下代码在 web2py 中定义了表格:
db.define_table('foods',
Field('name'),
Field('recipe_id', db.recipes, required=False, notnull=False, requires=None))
我相信我遇到的问题在于 web2py,因为我可以直接从 mysql 命令行提示符插入 Foods 表,而无需指定 recipe_id。
我在这里遗漏了什么吗?我是 MySQL 和 web2py 的新手 :(
【问题讨论】:
-
插入效果怎么样?