Opened 13 years ago

Closed 13 years ago

#16104 closed Bug (invalid)

inspectdb of a Mysql table fails to add null=True for Varchars

Reported by: barracel Owned by: nobody
Component: Core (Management commands) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Using inspectdb for the following table:

Code highlighting:

   CREATE TABLE `test` (
      `test_int` bigint DEFAULT NULL,
      `test_varchar` varchar(100) DEFAULT NULL);

I get the model:

Code highlighting:

  class Test(models.Model):
      test_int = models.BigIntegerField(null=True, blank=True)
      test_varchar = models.CharField(max_length=300, blank=True)
      class Meta:
          db_table = u'test'

test_varchar definition contains null=True but test_int does not

Change History (1)

comment:1 by Aymeric Augustin, 13 years ago

Resolution: invalid
Status: newclosed

Quoting https://docs.djangoproject.com/en/dev/ref/models/fields/#null:

Note that empty string values will always get stored as empty strings, not as NULL. Only use null=True for non-string fields such as integers, booleans and dates.


inspectdb implements this convention: it ensures that TextFields and CharFields never get null=True with the following code:

                if row[6]: # If it's NULL...
                    extra_params['blank'] = True
                    if not field_type in ('TextField(', 'CharField('):
                        extra_params['null'] = True


Quoting https://docs.djangoproject.com/en/1.3/ref/django-admin/#inspectdb:

This feature is meant as a shortcut, not as definitive model generation. After you run it, you'll want to look over the generated models yourself to make customizations.


Nothing prevents you from adding the null=True, but in general it's a bad idea and inspectdb won't do it — read the first link above to learn why.

As a conclusion, it's a feature, not a bug :)

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