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

时间:2014-04-07 15:27:53

标签: mysql django

我在1452错误上与Django和MySQL挣扎。

我正在尝试创建一个GoogleAnalyticsUserWeb属性对象,该对象的外键指向GoogleAnalyticsUserAccount。它失败了,但是当我尝试直接在MySQL中INSERT时,它运行正常。

以下是我的模特:

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')

相关的GoogleAnalyticsUserWeb属性模型的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:

给出的表格描述
  

|名称|发动机|版本|   Row_format |行| Avg_row_length | Data_length | Max_data_length |   Index_length | Data_free | Auto_increment | Create_time |   Update_time | Check_time |整理|校验和|   Create_options |评论|

     

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

     

digger_googleanalyticsuserwebproperty | InnoDB | 10 |紧凑型
  | 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,'无法添加或更新子行:外键约束失败(cpudigger_googleanalyticsuserwebproperty,CONSTRAINT related_account_id_refs_id_e855a0a4 FOREIGN KEY(related_account_id )参考digger_googleanalyticsuseraccountid))')

最后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(&#39; 48361394&#39;在Django&#39;生成的sql中,第二个参数)而不是digger_googleanalyticsuseraccount.id作为外键值。我怎样才能让Django使用&#39; id&#39; (digger_googleanalyticsuseraccount主键)而不是account_id ??

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我以某种方式弄清楚出了什么问题: GoogleAnalyticsUserWeb属性包含以下字段:&#34; account&#34; (外键) 和&#34; account_id&#34; (一个字符串)。 我改变了#34; account_id&#34;到&#34; webproperty_account_id&#34;它修复了意外的Django行为。仅供参考,我使用的是Django 1.6.2。

感谢您的帮助!