gpt4 book ai didi

mysql - Django 在 object.save() 上引发 MySQL 错误 1452,但 INSERT 有效

转载 作者:行者123 更新时间:2023-11-30 00:25:36 24 4
gpt4 key购买 nike

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

我正在尝试创建一个 GoogleAnalyticsUserWebproperty 对象,该对象具有指向 GoogleAnalyticsUserAccount 的外键。它失败了,但是当我尝试直接在 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 给出的表描述:

| Name | Engine | Version | Row_format | Rows | Avg_row_length | Data_length | Max_data_length | Index_length | Data_free | Auto_increment | Create_time | Update_time | Check_time | Collation | Checksum | Create_options | Comment |

digger_googleanalyticsuseraccount | InnoDB | 10 | Compact
| 1 | 16384 | 16384 | 0 | 32768 | 4194304 | 2 | 2014-04-07 16:40:36 | NULL | NULL | latin1_swedish_ci | NULL | | |

digger_googleanalyticsuserwebproperty | InnoDB | 10 | Compact
| 1 | 16384 | 16384 | 0 | 49152 | 4194304 | 4 | 2014-04-07 16:40:36 | NULL | NULL | latin1_swedish_ci | NULL | | | |

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, 'Cannot add or update a child row: a foreign key constraint fails (cpu.digger_googleanalyticsuserwebproperty, CONSTRAINT related_account_id_refs_id_e855a0a4 FOREIGN KEY (related_account_id) REFERENCES digger_googleanalyticsuseraccount (id))')

最后,INSERT 工作得很好:

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

因此,我确保这些表是 INNODB,并且存在 pk=1 的 GoogleAnalyticsUserAccount 对象。

我可能会错过什么?

编辑:

Python 代码中的帐户对象给了我:

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?

最佳答案

您可以将 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'],
[...])

关于mysql - Django 在 object.save() 上引发 MySQL 错误 1452,但 INSERT 有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22916747/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com