[Django] #29583: python manage.py inspectdb makes all fields with a default value blank=True, null=True

8 views
Skip to first unread message

Django

unread,
Jul 20, 2018, 5:24:14 PM7/20/18
to django-...@googlegroups.com
#29583: python manage.py inspectdb makes all fields with a default value
blank=True, null=True
-------------------------------------+-------------------------------------
Reporter: kimballo | Owner: nobody
Type: Bug | Status: new
Component: Core | Version: 2.0
(Management commands) | Keywords: manage.py,
Severity: Normal | inspectdb, models
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
Wasn't sure on the category or difficulty of fix, so logged it as an "easy
pickings bug".

I'm using:

Ubuntu 16.04
Python 3.7.0
Django 2.0.4
Mariadb 10.2.16

Before I get into this, I realize that
-"Django is best suited for developing new applications"
-This probably isn't a huge deal or a high priority
...BUT I figured I'd submit this anyway while I was thinking about it.
Maybe it'll save someone some extra work down the line.


Steps to reproduce:

1. Setup a legacy db in settings.py (see
https://docs.djangoproject.com/en/2.0/howto/legacy-databases/)
-Prerequisite: you have a column in one of your tables that has a default
value.
-Example: test_field int(11) DEFAULT 1
2. Run python manage.py inspectdb [--database DATABASE_NAME [ TABLE ] ] >
models.py (or don't include "> models.py" and you can view the results
from the command line)
3. Open models.py
-test_field will look something like this:
{{{test_field = models.IntegerField(blank=True, null=True)}}}
-What I would expect:
{{{test_field = models.IntegerField(default=1)}}}

What I've been able to dig up:

#
https://www.google.com/url?q=https%3A%2F%2Fgithub.com%2Fdjango%2Fdjango%2Fblob%2Fmaster%2Fdjango%2Fcore%2Fmanagement%2Fcommands%2Finspectdb.py%23L144&sa=D&sntz=1&usg=AFQjCNGZLkFkaqIJ1Ap1dhXRB0jARXcCoA
# from inspectdb.py (line 144, comments included in the original file, )


{{{
142 # Add 'null' and 'blank', if the 'null_ok' flag was present in the
143 # table description.
144 if row[6]: # If it's NULL...
145 extra_params['blank'] = True
146 extra_params['null'] = True
}}}


#
https://github.com/django/django/blob/master/django/db/backends/mysql/introspection.py#L95+info.column_default
# from introspection.py in get_field_description (line 95, comment is
mine)


{{{
86 fields = []
87 for line in cursor.description:
88 info = field_info[line[0]]
89 fields.append(FieldInfo(
90 *line[:3],
91 to_int(info.max_len) or line[3],
92 to_int(info.num_prec) or line[4],
93 to_int(info.num_scale) or line[5],
94 line[6],
95 info.column_default, # THIS LINE IS JUST WHATEVER THE
COLUMN DEFAULT IS
96 info.extra,
97 info.is_unsigned,
98 ))
99 return fields
}}}


I'm sure there are a lot of things I don't understand about this, but I
don't think that having a default value should automatically add
blank=True, null=True.
Is having a default value really a "'null_ok' flag"?

I think it should only put blank=True, null=True if the default is set to
NULL in the column. Otherwise, it should set the default to whatever the
default value is (ex: default=1)

Caveat: I've only tested this with int fields.

--
Ticket URL: <https://code.djangoproject.com/ticket/29583>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 20, 2018, 6:38:48 PM7/20/18
to django-...@googlegroups.com
#29583: python manage.py inspectdb makes all fields with a default value
blank=True, null=True
-------------------------------------+-------------------------------------
Reporter: Kimball Leavitt | Owner: nobody
Type: Bug | Status: closed
Component: Core (Management | Version: 2.0
commands) |
Severity: Normal | Resolution: invalid
Keywords: manage.py, | Triage Stage:
inspectdb, models | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => invalid


Comment:

I think you made a mistake in your analysis. In the `FieldInfo`
initialization, `line[6]` (not `info.column_default`) corresponds to
`row[6]` in `inspectdb`. In your example, `test_field int(11) DEFAULT 1`
seems to be nullable (according to the MySQL documentation, "If neither
NULL nor NOT NULL is specified, the column is treated as though NULL had
been specified." -- I guess it's the same for MariaDB.

As [https://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb the
documentation] says, "database defaults aren’t translated to model field
defaults or detected in any fashion by inspectdb."

--
Ticket URL: <https://code.djangoproject.com/ticket/29583#comment:1>

Reply all
Reply to author
Forward
0 new messages