#25343: pk attribute not set when having own primary key
-------------------------------------+-------------------------------------
     Reporter:  IlianIliev           |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  1.8
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by IlianIliev):

 Replying to [comment:4 carljm]:
 > Is this really your exact code? Unless you have `null=True` on the
 `language` field, that code should raise a database error like "null value
 not allowed in column 'language'".
 >
 > But it looks like your database is automatically setting a value for
 that field instead of raising an error. So maybe your database schema
 doesn't match your model definition? What database are you using, and what
 is the actual schema for that table? Did Django create it, or did you
 create it manually?
 >
 > If you set a value for the `language` field, that will be the value of
 the `pk` attribute.
 >
 > If you don't want to have to explicitly set a value for the primary key,
 you should be using `AutoField`, not `IntegerField` (or just let Django
 automatically add an `AutoField` named `id`, as it does if you don't set
 `primary_key=True` on any of your fields).


 No actually the exact code is:

 {{{
 class Language(models.Model):
     language = models.IntegerField(verbose_name='Language',
 primary_key=True) #, choices=LANGUAGES, max_length=2)
     code = models.CharField(verbose_name='Language code (2 chars)',
 blank=False, max_length=2)
     priority = models.PositiveIntegerField(verbose_name="Priority",
 default=0, null=False, blank=True)
 }}}

 it is a legacy code, and I am not sure how the DB has been created but I
 can reproduce it if I recreate the table with syncdb in a new DB (sqlite).
 The value is automatically autoincremented.
 I also found the the `language` property is also not set in the object
 after create. However after getting the object from the DB everything is
 fine.

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

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/068.d3c44c40165d22f1ac05bb97ae0f188f%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to