#28952: inspectdb generates primary_key with null=True
-------------------------------------+-------------------------------------
               Reporter:  Victor     |          Owner:  nobody
  Fernandez Martinez                 |
                   Type:  Bug        |         Status:  new
              Component:  Core       |        Version:  1.11
  (Management commands)              |       Keywords:  inspectdb
               Severity:  Normal     |  primary_key null
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I have a PostgreSQL 9.6 table that looks like this:

 {{{
 CREATE TABLE admin_item_permissions
 (
   id bigint NOT NULL DEFAULT nextval('next_unique_id_seq'::regclass),
   admin_id bigint,
   item_id bigint,
   permissions integer,
   CONSTRAINT admin_item_permissions_pkey PRIMARY KEY (id)
 )
 WITH (
   OIDS=FALSE
 );

 CREATE UNIQUE INDEX admin_perms_item_id_admin_id
   ON admin_item_permissions
   USING btree
   (admin_id, item_id);
 }}}


 When I run inspectdb, it generates the following model:


 {{{
 class AdminItemPermissions(models.Model):
     id = models.BigIntegerField(primary_key=True, blank=True, null=True)
     admin_id = models.BigIntegerField(blank=True, null=True)
     item_id = models.BigIntegerField(blank=True, null=True)
     permissions = models.IntegerField(blank=True, null=True)

     class Meta:
         managed = False
         unique_together = (('admin_id', 'item_id'),)
 }}}


 The "id" field generated by inspectdb contains "null=True", even though
 it's a primary key and the "id" column explicitly has "NOT NULL". I would
 expect it not to add "null=True" instead, or to add "null=False".

 This also causes creation of a test database to fail when trying to run
 unit tests, with the following error message:

 {{{
 ERRORS:
 common.AdminItemPermissions.id: (fields.E007) Primary keys must not have
 null=True.
         HINT: Set null=False on the field, or remove primary_key=True
 argument.
 }}}

-- 
Ticket URL: <https://code.djangoproject.com/ticket/28952>
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/066.7ed82f5e4cd0c4194d7d750270030489%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to