Code

Opened 3 years ago

Closed 3 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:

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

Attachments (0)

Change History (1)

comment:1 Changed 3 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

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 :)

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.