[Django] #23215: Warning: Field 'id' doesn't have a default value

13 views
Skip to first unread message

Django

unread,
Aug 5, 2014, 9:53:02 AM8/5/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
----------------------------------------------+----------------------------
Reporter: duduklein | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.7-rc-2
Severity: Normal | Keywords: model default
Triage Stage: Unreviewed | value
Easy pickings: 0 | Has patch: 0
| UI/UX: 0
----------------------------------------------+----------------------------
This is a problem that I don't have with django 1.7.b4, but I do have it
with 1.7c2.

I have a model like this:

{{{
class Image(models.Model):
id = models.AutoField(primary_key=True)
datetime = models.DateTimeField(default=datetime.now)
file = models.FileField()
#some other fields
}}}


At some point of my code, I do:

{{{
image = Image()
...
image.file.save(filename, file_content)
image.save()
}}}

This works fine with django 1.7.b4, but gives the following error (even if
it says warning):
"Field 'id' doesn't have a default value"

My db backend is MySQL.

This seems to be a bug, since I did not find anything in the release notes
that says that this was not possible in django 1.7

I have tried to create a test model to reproduce the problem, but all the
tests I made with the 3 fields above only passed (no errors). There must
be some specific condition in my model that makes it fails.

Since you know what has changed from 1.7.b4 to 1.7c2, do yoy know or have
an idea of what maybe happening. If you give me directions on what have
changed, I can make more tests to reproduce the bug.

--
Ticket URL: <https://code.djangoproject.com/ticket/23215>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 5, 2014, 10:16:41 AM8/5/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------

Reporter: duduklein | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

No ideas, you could try bisecting to find the commit that introduced the
change. What code is triggering the warning?

--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:1>

Django

unread,
Aug 5, 2014, 4:16:02 PM8/5/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------

Reporter: duduklein | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by CollinAnderson):

Is your `id` field actually `auto_increment` in the database? Did you
create the `id` field first, then switch it to an `AutoField` later?

run `python manage.py dbshell` then `show columns in nameofyourapp_image;`
and see if it has `auto_increment` next to the `id` field under `Extra`.

--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:2>

Django

unread,
Aug 5, 2014, 4:16:15 PM8/5/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------

Reporter: duduklein | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by CollinAnderson):

* cc: cmawebsite@… (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:3>

Django

unread,
Aug 5, 2014, 4:57:12 PM8/5/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------

Reporter: duduklein | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by duduklein):

Hi,
My id has the autoincrement as well. Here is the output.
Field | Type | Null | Key | Default | Extra |
+-----+--------------+------+-----+---------+----------------+
| id | int(11)| NO | PRI | NULL | auto_increment

But you may have given a hint to the issue. When I first created the
model, I did not declare the id field in it, so django has created it
automatically. Then, later on, I added it and ran the migrate, that
obviously did not change the db.

I'm going to debug more and try to find the cause. I'll try bisecting it
(although never used before).

I'll post my findings as soon as I have something interesting.

--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:4>

Django

unread,
Aug 12, 2014, 8:01:43 AM8/12/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------

Reporter: duduklein | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by BlindHunter):

Simply mysqldump and restore your database from dump.

--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:5>

Django

unread,
Aug 14, 2014, 6:15:09 PM8/14/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------

Reporter: duduklein | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution:
Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by RoninDusette):

Any time you make a change to your models, like say, adding/changing a
field that is required, you HAVE to give it a default value.

Think about it. If null=True and blank=True are not set on that field,
django will not want to save it to the DB, and it will require a non-NULL
piece of data to hold in there.

When I ran across this, it scared me, so what I did was just add
blank=True and null=True to those specific fields, and then I would have
to do a little work to bring the existing tables up-to-date with it, then
I would go remove blank/null=True on that field, sync it up, before ANY
new entries are made. After that it should be good. This is not a
programming issue nor a bug. It is more of a lack of experience doing
migrations.

As BlindHunter said, you should just revert your changes to your models
and restore your database from backup.... You DO use Git or something for
your code, and you DID backup your database, didn't you? :D If not, this
is a good experience to learn from. haha.

Also, don't add an ID field. Django creates and auto-incrementing ID field
for you already.

--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:6>

Django

unread,
Aug 22, 2014, 2:14:39 PM8/22/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------
Reporter: duduklein | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution: needsinfo

Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => needsinfo


--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:7>

Django

unread,
Oct 21, 2014, 3:03:58 AM10/21/14
to django-...@googlegroups.com
#23215: Warning: Field 'id' doesn't have a default value
-------------------------------------+-------------------------------------
Reporter: duduklein | Owner: nobody

Type: Bug | Status: closed
Component: Database layer | Version: 1.7-rc-2
(models, ORM) | Resolution: needsinfo
Severity: Normal | Triage Stage:
Keywords: model default value | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by duduklein):

Sorry for the delay in responding.

I could not reproduce the issue, but the solution I used is described
here: http://webit.ca/2012/01/field-id-doesnt-have-a-default-value/
I found a ticket in south that may be related:
http://south.aeracode.org/ticket/1160
Maybe, django's migrations behave the same way

--
Ticket URL: <https://code.djangoproject.com/ticket/23215#comment:8>

Reply all
Reply to author
Forward
0 new messages