Opened 8 years ago

Closed 8 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


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 Changed 8 years ago by Aymeric Augustin

Resolution: invalid
Status: newclosed


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


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