【问题标题】:Django raises MySQL error 1452 on object.save() but INSERT worksDjango 在 object.save() 上引发 MySQL 错误 1452 但 INSERT 有效
【发布时间】:2014-05-19 22:50:18
【问题描述】:

我在 Django 和 MySQL 中遇到 1452 错误。

我正在尝试创建一个具有指向 GoogleAnalyticsUserAccount 的外键的 GoogleAnalyticsUserWebproperty 对象。它失败了,但是当我尝试直接在 MySQL 中插入时,它工作正常。

这是我的模型:

class GoogleAnalyticsUserAccount(models.Model):
    user = models.ForeignKey(User)
    account_id = models.CharField(max_length=64)
    [...]

    class Meta:
        unique_together = ('user', 'account_id')


    class GoogleAnalyticsUserWebproperty(models.Model):
        user = models.ForeignKey(User)
        account = models.ForeignKey(GoogleAnalyticsUserAccount,
                                    db_column='related_account_id',
                                    on_delete=models.CASCADE)
        webproperty_id = models.CharField(max_length=64)

        class Meta:
            unique_together = ('user', 'account', 'webproperty_id')

相关的 GoogleAnalyticsUserWebproperty 模型的 MySQL 表:

+--------------------------+--------------+------+-----+---------+----------------+
| Field                    | Type         | Null | Key | Default | Extra          |
+--------------------------+--------------+------+-----+---------+----------------+
| id                       | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id                  | int(11)      | NO   | MUL | NULL    |                |
| related_account_id       | int(11)      | NO   | MUL | NULL    |                |
| webproperty_id           | varchar(64)  | NO   |     | NULL    |                |
[...]
+--------------------------+--------------+------+-----+---------+----------------+

SHOW TABLE STATUS 给出的表描述:

|姓名 |引擎 |版本 | 行格式 |行 |平均行长度 |数据长度 |最大数据长度 | 索引长度 |数据免费 |自动增量 |创建时间 | 更新时间 |检查时间 |整理 |校验和 | 创建选项 |评论 |

digger_googleanalyticsuseraccount |数据库 | 10 |紧凑型
| 1 | 16384 | 16384 | 0 | 32768 | 4194304 | 2 | 2014-04-07 16:40:36 |空 | 空 | latin1_swedish_ci |空 | | |

digger_googleanalyticsuserwebproperty |数据库 | 10 |紧凑型
| 1 | 16384 | 16384 | 0 | 49152 | 4194304 | 4 | 2014-04-07 16:40:36 |空 | 空 | latin1_swedish_ci |空 | | | |

Django 的代码失败(找到了 GoogleAnalyticsUserAccount,所以这里应该没有任何问题):

for webproperty in webproperties:
    try:
        GoogleAnalyticsUserWebproperty.objects.get(user=user,
                                                   webproperty_id=webproperty['id'])
    except GoogleAnalyticsUserWebproperty.DoesNotExist:
        a = GoogleAnalyticsUserAccount.objects.get(user=user,
                                                   account_id=webproperty['accountId'])

        w = GoogleAnalyticsUserWebproperty(user=user,
                                           account=a,
                                           webproperty_id=webproperty['id'],
                                           [...])

        w.save()

Django的相关错误信息:

(1452, '无法添加或更新子行:外键约束失败 (cpu.digger_googleanalyticsuserwebproperty, CONSTRAINT related_account_id_refs_id_e855a0a4 FOREIGN KEY (related_account_id) REFERENCES digger_googleanalyticsuseraccount (id)) ')

最后插入效果很好:

insert into digger_googleanalyticsuserwebproperty values(1, 1, 1, 1, [...]);

所以我确保表是 INNODB 并且存在 pk=1 的 GoogleAnalyticsUserAccount 对象。

我会在这里遗漏什么?

编辑:

Python 代码中的 account 对象给了我:

print(a) # "boom-bike.info 48361394 (admin)" (from custom __unicode__)
print(a.pk) # 1

堆栈跟踪的一个有趣部分:

/var/local/virtualenvs/cpu-dev/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py in execute_sql
            cursor.execute(sql, params) ... 
▼ Local vars Variable   
Value cursor     
<django.db.backends.util.CursorDebugWrapper object at 0xa65b0ec> self    
<django.db.backends.mysql.compiler.SQLInsertCompiler object at 0xa65b94c> 
return_id    True 
params   [1,  48361394,  1,  u'XXX',  u'STANDARD',  u'79873902',  u'ONLINE_COMMUNITIES',  u'effective',  u'UA-XXX',  u'analytics#webproperty',  u'48361394',  u'http://boom-bike.info',  u'https://www.googleapis.com/analytics/v3/management/accounts/XXX/webproperties/UA-48361394-1', u'2014-02-24 12:16:30',  u'2014-02-24 12:16:36',  u'https://www.googleapis.com/analytics/v3/management/accounts/XXX', u'analytics#account',  u'https://www.googleapis.com/analytics/v3/management/accounts/XXX/webproperties/UA-48361394-1/profiles', u'analytics#profiles'] 
sql  u'INSERT INTO `digger_googleanalyticsuserwebproperty` (`user_id`, `related_account_id`, `profile_count`, `name`, `level`, `internal_web_property_id`, `industry_vertical`, `permissions`, `webproperty_id`, `kind`, `account_id`, `website_url`, `self_link`, `created`, `updated`, `parent_link_href`, `parent_link_type`, `child_link_href`, `child_link_type`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)'

所以似乎使用了 digger_googleanalyticsuseraccount.account_id(Django 生成的 sql 中的'48361394',第二个参数)而不是 digger_googleanalyticsuseraccount.id 作为外键值。如何让 Django 使用“id”(digger_googleanalyticsuseraccount 主键)而不是 account_id??

【问题讨论】:

  • 嗯,再调试一下,放入打印语句或使用pdb 来查看尝试创建GoogleAnalyticsUserWebproperty 对象时到底发生了什么。看看a的值是多少,看看有没有pk。您的循环中的一个特定 webproperty 可能存在问题
  • 谢谢Sohan,我在回溯中发现了一个有趣的线索。我已经相应地更新了我的帖子。
  • MySQL 给了我关于 GoogleAnalyticsUserWebproperty 的以下信息:KEY digger_googleanalyticsuserwebproperty_93025c2f (related_account_id), CONSTRAINT related_account_id_refs_id_e855a0a4 FOREIGN KEY (related_account_id) REFERENCES digger_googleanalyticsuseraccount (id) 所以 MySQL 很好映射但 Django 无法确定它必须使用 digger_googleanalyticsuseraccount.pk 而不是 digger_googleanalyticsuseraccount.account_id 即使指定 to_field='id'。

标签: mysql django


【解决方案1】:

您可以将related_account_id 显式设置为a.id,如下所示:

w = GoogleAnalyticsUserWebproperty(user=user,
                                   related_account_id=a.id, #might be account_id instead of related_account_id
                                   webproperty_id=webproperty['id'],
                                   [...])

【讨论】:

  • 它不起作用(TypeError - 'related_account_id' 是此函数的无效关键字参数)
  • 我有一个名为 account 的字段和另一个名为 account_id 的字段。他们正在干扰,导致意外的 Django 行为。
【解决方案2】:

好吧,我不知何故弄明白出了什么问题: GoogleAnalyticsUserWebproperty 包含以下字段:“帐户”(外键) 和“account_id”(一个字符串)。 我将“account_id”更改为“webproperty_account_id”,它修复了意外的 Django 行为。仅供参考,我使用的是 Django 1.6.2。

感谢您的帮助!

【讨论】:

    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 2019-11-26
    • 2013-11-15
    • 2013-02-15
    • 2016-02-17
    • 2014-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多