[英]IntegrityError when adding null value to foreign key using web2py
I am receiving an IntegrityError from web2py when trying to insert a record into a table named Foods .尝试将记录插入名为Foods的表时,我收到来自 web2py 的 IntegrityError。 The table has a foreign key to Recipes , but I would like to add a row without a recipe_id.该表有一个外键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)'))
This is my table这是我的桌子
+-----------+--------------+------+-----+---------+----------------+
| 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 | |
+-----------+--------------+------+-----+---------+----------------+
I have defined the table in web2py using the following code:我使用以下代码在 web2py 中定义了表:
db.define_table('foods',
Field('name'),
Field('recipe_id', db.recipes, required=False, notnull=False, requires=None))
I believe the problem I am having lies with web2py as I can insert into the Foods table directly from the mysql command line prompt without specifying a recipe_id.我相信我遇到的问题在于 web2py,因为我可以直接从 mysql 命令行提示符插入Foods表,而无需指定 recipe_id。
Am I missing something here?我在这里错过了什么吗? I'm new to MySQL and web2py:(我是 MySQL 和 web2py 的新手:(
Make sure your Foods and Recipes have same engines.确保您的食物和食谱具有相同的引擎。 If you have different engines, then this problem will be occured.如果你有不同的引擎,那么就会出现这个问题。
Eg: Foods (MyISAM) and Recipes (InnoDB) will give error.例如:Foods (MyISAM) 和 Recipes (InnoDB) 会出错。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.