在Django中保存模型给我“Warning:Field”id'没有默认值“ [英] Saving a model in Django gives me "Warning: Field 'id' doesn't have a default value"

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

问题描述

我在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,标记为主键和自动增量。我觉得奇怪的是得到这个警告,因为DB和模型结束的一切都很酷。错误是:

  _mysql_exceptions.Warning 

警告:字段'id'没有默认值

保存时的工作是将id设置为0.它会使警告和自从MySQL处理它以来,id被正确设置。

  case = Case()
case.id = 0 #ugly解决方法为 - 警告:字段'id'没有默认值
case.name = request.POST [u'case [name]']
case.save()

此解决方案的问题是:


  1. 这是丑的

  2. 新的PK在保存后无法访问,这使得
    无法正常序列化

有没有人知道如何解决这个问题?



我正在运行:

  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)使用readline 5.1
MySQL_py thon-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定义显​​示:


  1. 模型影响没有明确定义PK或明确定义了PK(都有问题)。这似乎无关紧要。


  2. 受影响的表与OP中的id定义相同,即 id int(11)NOT NULL AUTO_INCREMENT PRIMARY KEY 按预期设置。


  3. 查看MySQL查询日志我有一些这样的程度:

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

    单独运行查询是成功的, SHOW WARNINGS 反映了OP中提到的警告。


然而,我遇到了一个MySQL端修复程序, http://webit.ca/2012/01/field-id-doesnt-have-a-default-value/ 提到,重置id定义(粗略一瞥似乎没有任何不同)解决了这个问题。我无法解释为什么这样做,但它解决了这个问题,没有任何额外的代码更改。我相信这可能在某种程度上与删除记录有关,但是整个问题对我来说似乎是不合理的。


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'

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

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

The issue with this solution is:

  1. It's ugly
  2. The new PK is inaccessible after saving which makes it impossible to serialize properly

Does anyone know how to fix this?

I'm running:

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

And my create table looks like this:

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

解决方案

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. The models affected either had no PK explicitly defined or had a PK explicitly defined (both had problems). This alone seemed unrelated.

  2. 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.

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

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

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

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中保存模型给我“Warning:Field”id'没有默认值“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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