【问题标题】:IntegrityError when adding null value to foreign key using web2py使用 web2py 将空值添加到外键时出现 IntegrityError
【发布时间】: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 的新手 :(

【问题讨论】:

  • 插入效果怎么样?

标签: python mysql web2py


【解决方案1】:

确保您的食物和食谱具有相同的引擎。如果你有不同的引擎,那么就会出现这个问题。

例如:Foods (MyISAM) 和 Recipes (InnoDB) 会报错。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2012-04-19
    • 2020-12-21
    • 2012-05-10
    • 2014-02-20
    • 1970-01-01
    相关资源
    最近更新 更多