Opened 9 years ago

Closed 7 years ago

#24365 closed Bug (fixed)

inspectdb should generate PositiveIntegerFields for unsigned fields.

Reported by: Bharadwaj Srigiriraju Owned by: Mariusz Felisiak
Component: Core (Management commands) Version: dev
Severity: Normal Keywords: inspectdb, unsigned, positiveintegerfield, mysql
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have tested this on MySQL and Django 1.7.4.

I have unsigned integer fields in my mysql table (which can be created from SQL like this: entity_id int(10) unsigned NOT NULL COMMENT 'Entity ID'), and when I introspect a table that contains fields like this, the corresponding model field generated is:

entity_id = models.IntegerField(),

whereas it should be something like:

entity_id = models.PositiveIntegerField().

This is same in case of unsigned smallint fields too.

A bug like this also breaks ForeignKey relationships on that field, as the new field created would be a normal INT and results in MySQL error 150 during migrations and breaks them. Changing the fields to PositiveIntegerField in generated file manually seems like the only fix for now (which was suggested to me on #django IRC).

So, inspectdb should generate PositiveIntegerField when the field is unsigned. I am not sure if this is the same for other databases too, as I haven't tested against them.

Change History (10)

comment:1 by Claude Paroz, 9 years ago

Component: Database layer (models, ORM)Core (Management commands)
Triage Stage: UnreviewedAccepted
Version: 1.7master

comment:2 by Claude Paroz, 9 years ago

Keywords: mysql added

AFAIK, this is a MySQL specific issue, as other backends don't care about signed/unsigned.

in reply to:  2 comment:3 by Bharadwaj Srigiriraju, 9 years ago

Replying to claudep:

AFAIK, this is a MySQL specific issue, as other backends don't care about signed/unsigned.

Is this issue an easy picking?

If yes, I would like to fix this myself. :)

comment:4 by Claude Paroz, 9 years ago

Feel free to try. I can assist you if needed.

comment:5 by Saishruti Shetty, 9 years ago

Owner: changed from nobody to Saishruti Shetty
Status: newassigned

comment:6 by Simon Charette, 9 years ago

Has patch: set
Patch needs improvement: set

comment:7 by Tim Graham, 8 years ago

Owner: Saishruti Shetty removed
Status: assignednew

comment:8 by Mariusz Felisiak, 7 years ago

Has patch: unset
Owner: set to Mariusz Felisiak
Patch needs improvement: unset
Status: newassigned

comment:9 by Mariusz Felisiak, 7 years ago

Has patch: set

comment:10 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 94d8bea2:

Fixed #24365 -- Made inspectdb translate MySQL unsigned integer columns to positive integer fields.

Note: See TracTickets for help on using tickets.
Back to Top