﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
28952	inspectdb generates primary_key with null=True	Victor Fernandez Martinez	nobody	"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.
}}}
"	Bug	closed	Core (Management commands)	1.11	Normal	worksforme	inspectdb primary_key null		Unreviewed	0	0	0	0	0	0
