在Django中保存模型会给我& quot;警告:字段' id'没有默认值" [英] Saving a model in Django gives me "Warning: Field 'id' doesn't have a default value"

查看:49
本文介绍了在Django中保存模型会给我& quot;警告:字段' id'没有默认值"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django中有一个非常基本的模型:

I have a very basic model in Django:

class Case(models.Model):
    name = models.CharField(max_length=255)
    created_at = models.DateTimeField(default=datetime.now)
    updated_at = models.DateTimeField(default=datetime.now)

    def save(self):
        if self.created_at == None:
             self.created_at = datetime.now()
        self.updated_at = datetime.now()
        super(Case, self).save()

    class Meta:
        db_table = u'cases'

因为我没有指定PK,所以Django为我负责.我在数据库中看到称为"id"的字段,该字段标记为主键并自动递增.我发现收到该警告很奇怪,因为数据库和模型端的一切都很酷.错误是:

Because I did not specify the PK, Django took care of that for me. I see the field in my database which is called "id", marked as a primary key and auto-increment. I find odd to be getting that warning since everything is cool on the DB and model end. The error is:

_mysql_exceptions.Warning

Warning: Field 'id' doesn't have a default value

在保存时,我的解决方法是将id设置为0.这会终止警告,并且自从MySQL处理该id以来,它就已经正确设置了.

My work around when saving is to set the id to 0. It kills the warning and the id gets set properly anyway since MySQL handles it.

case = Case()
case.id = 0 #ugly workaround for - Warning: Field 'id' doesn't have a default value
case.name = request.POST[u'case[name]']
case.save()

此解决方案的问题是:

  1. 丑陋
  2. 保存后无法访问新的PK,这使其成为无法正确序列化

有人知道如何解决此问题吗?

Does anyone know how to fix this?

我正在跑步:

Python 2.7.2
django.VERSION (1, 3, 1, 'final', 0)
mysql  Ver 14.14 Distrib 5.1.45, for apple-darwin10.2.0 (i386) using readline 5.1
MySQL_python-1.2.3-py2.7-macosx-10.4-x86_64.egg

我的创建表如下:

CREATE TABLE `cases` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=latin1

推荐答案

我想在本周遇到的这个问题上进行扩展.我遇到了同样的确切错误,并查看了模型定义和SQL定义,结果表明:

I'd like to expand on this question as I've ran into it this week. I was getting this same exact error and taking a look at the model definition and SQL definition showed that:

  1. 受影响的模型要么未明确定义PK,要么已明确定义PK(均存在问题).仅此一项似乎无关.

  1. The models affected either had no PK explicitly defined or had a PK explicitly defined (both had problems). This alone seemed unrelated.

受影响的表具有与OP中相同的"id"定义,即 id int(11)NOT NULL AUTO_INCREMENT . PRIMARY KEY 已设置为预期值.

The tables affected had the "id" definition as in the OP, i.e. id int(11) NOT NULL AUTO_INCREMENT. The PRIMARY KEY is set as expected.

在MySQL查询日志中,我得到了某种程度的帮助:

Looking into the MySQL query log I got something to this extent:

INSERT INTO `x` (...) VALUES (...)
SHOW WARNINGS
ROLLBACK

单独运行查询成功,并且 SHOW WARNINGS 反映了OP中记录的警告.

Running the query individually is successful, and SHOW WARNINGS reflects the warning noted in the OP.

但是我遇到了一个MySQL端修复程序, http://webit.ca/2012/01/field-id-doesnt-have-a-default-value/提到重置ID定义(粗略地看似乎没有什么不同)可以解决此问题.我无法解释为什么会这样,但是无需任何其他代码更改即可解决该问题.我认为这可能与删除记录有关,但是整个问题对我来说似乎是无意义的.

I however came across a MySQL-end fix, http://webit.ca/2012/01/field-id-doesnt-have-a-default-value/ mentioned that resetting the id definition (which at a cursory glance appears to not be any different) fixes the problem. I can't explain why this works, but it addresses the problem without any additional code changes. I believe this may be in some way linked to removing records, but the whole issue seems non-sensical to me.

这篇关于在Django中保存模型会给我& quot;警告:字段' id'没有默认值"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆