使用 web2py 向外键添加 null 值时出现 IntegrityError
当我尝试将记录插入名为 Foods 的表时,我收到来自 web2py 的 IntegrityError。该表有一个食谱的外键,但我想添加一行没有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 表,而无需指定食谱 ID。
我在这里错过了什么吗?我是 MySQL 和 web2py 的新手:(
I am receiving an IntegrityError from web2py when trying to insert a record into a table named Foods. The table has a foreign key to Recipes, but I would like to add a row without a 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:
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.
Am I missing something here? I'm new to MySQL and web2py :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您的食物和食谱具有相同的引擎。如果你有不同的引擎,那么就会出现这个问题。
例如:Foods (MyISAM) 和 Recipes (InnoDB) 将给出错误。
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.