【发布时间】: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, CONSTRAINTrelated_account_id_refs_id_e855a0a4FOREIGN KEY (related_account_id) REFERENCESdigger_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), CONSTRAINTrelated_account_id_refs_id_e855a0a4FOREIGN KEY (related_account_id) REFERENCESdigger_googleanalyticsuseraccount(id) 所以 MySQL 很好映射但 Django 无法确定它必须使用 digger_googleanalyticsuseraccount.pk 而不是 digger_googleanalyticsuseraccount.account_id 即使指定 to_field='id'。