Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django在object.save()上引发MySQL错误1452,但INSERT有效_Mysql_Django - Fatal编程技术网

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

Django在object.save()上引发MySQL错误1452,但INSERT有效,mysql,django,Mysql,Django,我正在努力解决Django和MySQL的一个1452错误 我正在尝试创建一个GoogleAnalyticsUserWebproperty对象,它有一个指向GoogleAnalyticsUserAccount的外键。它失败了,但是当我尝试直接在MySQL中插入时,它就可以正常工作了 以下是我的模型: class GoogleAnalyticsUserAccount(models.Model): user = models.ForeignKey(User) account_id =

我正在努力解决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    |                |
[...]
+--------------------------+--------------+------+-----+---------+----------------+
“显示表格状态”给出的表格说明:

|名称|引擎|版本| 行格式|行|平均行|长度|数据|长度|最大数据|长度| 索引|长度|数据|自由|自动|增量|创建|时间| 更新|时间|检查|时间|排序|校验和| 创建|选项|注释|

digger_googleanalyticsuseraccount | InnoDB | 10 | Compact
| 1 | 16384 | 16384 | 0 | 32768 |4194304 | 2 | 2014-04-07 16:40:36 |空| 空|拉丁语|瑞典语| ci |空||

digger_googleanalyticsuserwebproperty|InnoDB|10|Compact
| 1 | 16384 | 16384 | 0 | 49152 |4194304 | 4 | 2014-04-07 16:40:36 |空| 空|拉丁语|瑞典语| ci |空|| |

Django的代码失败(发现一个GoogleAnalyticsUserAccount,因此这里应该没有任何问题):

Django的相关错误消息:

(1452,'无法添加或更新子行:外键约束失败(
cpu
digger\u googleanalyticsuserwebproperty
,约束
related\u account\u id\u refs\u id\u e855a0a4
外键(
related\u account\u id
)引用
digger\u 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)'
执行sql中的
/var/local/virtualenvs/cpu-dev/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py
cursor.execute(sql,params)。。。
▼ 局部变量
值光标
自己
返回\u id True
params[148361394,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‘你’https://www.googleapis.com/analytics/v3/management/accounts/XXX/webproperties/UA-48361394-1’,u'2014-02-2412:16:30',u'2014-02-2412: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'插入到'digger\u googleanalyticsuserwebproperty'中(`user\u id`、related\u account\u id`、profile\u count`、name`、level`、internal\u web\u property\u id`、industry\u vertical`、permissions`、webproperty\u id`、kind`、account\u id`、website\u url`、self\u link`、created`、updated`、parent\u link\u href`、parent\u link\u type`、child\u link\u href`)值(%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s、%s))

因此,似乎使用digger_googleanalyticsuseraccount.account_id('48361394'在Django生成的sql中,第二个参数)而不是digger_googleanalyticsuseraccount.id作为外键值。我如何让Django使用'id'(digger_googleanalyticsuseraccount主键)您可以显式地将相关的帐户id设置为
a.id,而不是帐户id???

,如下所示:

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

好吧,我不知怎么搞明白了问题所在: GoogleAnalyticsUserWebproperty包含以下字段:“帐户”(外键) 和“account_id”(一个字符串)。 我将“account\u id”更改为“webproperty\u account\u id”,它修复了意外的Django行为。仅供参考,我使用的是Django 1.6.2


感谢您的帮助!

Hmm若要进行更多调试,请输入打印语句或使用
pdb
查看在尝试创建
GoogleAnalyticsUserWebproperty
对象时到底发生了什么。查看
a
的值,并查看它是否有pk。某个特定的
webproperty
可能存在问题>谢谢Sohan,我在回溯中发现了一条有趣的线索。我已经相应地更新了我的帖子。MySQL为我提供了关于GoogleAnalyticsUserWebproperty的以下信息:KEY
digger\u GoogleAnalyticsUserWebproperty\u 93025c2f
相关账户\u id
),约束
related\u account\u id\u refs\u id\u e855a0a4
外键(
related\u account\u id
)引用
digger\u googleanalyticsuseraccount
id
)因此,MySQL映射良好,但Django无法确定它必须使用digger\u googleanalyticsuseraccount.pk而不是digger\u googleanalyticsuseraccount.account\u id,即使在指定为\u field='id'时也是如此。它不起作用(TypeError-'related\u account\u id'是此函数的无效关键字参数)我有一个名为account的字段和另一个名为account\u id的字段。它们进行了干扰,导致了意外的Django行为。
w = GoogleAnalyticsUserWebproperty(user=user,
                                   related_account_id=a.id, #might be account_id instead of related_account_id
                                   webproperty_id=webproperty['id'],
                                   [...])