Opened 15 years ago
Closed 12 years ago
#12460 closed Bug (fixed)
inspectdb and field names ending with underscores
Reported by: | Owned by: | kgibula | |
---|---|---|---|
Component: | Core (Management commands) | Version: | dev |
Severity: | Normal | Keywords: | inspectdb underscore characters digits |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
Hi,
There is a field named 'type_' in mysql table. Inspectdb inspects it as 'type_' and model does not validate. I suggest removing underscores from model field name and specifying column_db argument in field constructor.
< type_ = models.CharField(max_length=90, db_column='type_')
type = models.CharField(max_length=90, db_column='type_')
Sorry for posting without login. I've forgot my password and didn't find restoring feature on this site.
Attachments (4)
Change History (15)
comment:1 by , 15 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 15 years ago
Django already handles the case of a python keyword: http://code.djangoproject.com/browser/django/trunk/django/core/management/commands/inspectdb.py#L64 . Having something with the same name as a builtin shouldn't matter.
comment:3 by , 15 years ago
Keywords: | characters digits added |
---|
There are a few other scenarios where invalid field names could be generated via the inspectdb command. For instance, a column name of "Foo/Bar", while unconventional, is a valid column name, at least in MySQL 5, but the invalid field name of 'foo/bar' would be generated.
Currently, inspectdb ensures that:
- field names do not contain spaces or dashes
- field names are not Python keywords
The patch I just attached improves this by also ensuring that:
- field names do not contain any characters outside of a-zA-Z0-9_
- field names do not contain double underscores
- field names neither start nor end with underscores
- field names do not start with digits
by , 15 years ago
Attachment: | inspect_db_py_names.patch added |
---|
Patch that addresses trailing underscore issue as well as several other introspection field naming scenarios
comment:4 by , 15 years ago
Has patch: | set |
---|
comment:5 by , 14 years ago
Needs tests: | set |
---|
comment:6 by , 14 years ago
Component: | django-admin.py inspectdb → Core (Management commands) |
---|
comment:7 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:8 by , 14 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
Patch can't be applied to trunk anymore. It would also convert both ''Foo__'' and ''foo_'' (valid MySQL column names) to ''foo_field''.
comment:9 by , 14 years ago
Easy pickings: | unset |
---|---|
Patch needs improvement: | set |
I tested it on MySQL, PostgreSQL and SQLite. Currently tests pass only on MySQL and PostgreSQL. When db_column keyword argument is added, on MySQL and PostgreQSL it looks like this: db_column='name_of_field'. On SQLite however it looks like this: db_column=u'name_of_field'. I am not sure which one is correct.
comment:10 by , 13 years ago
Needs tests: | unset |
---|---|
Patch needs improvement: | unset |
UI/UX: | unset |
Version: | 1.1 → SVN |
Here's an updated patch where I have slightly refactored things to isolate the normalization process of the field name. I purposely did not include the non-ascii characters issue which was also addressed by previous "inspectdb.diff" patch. If the committer finds it's better to also include it, I can complete the patch. Otherwise, I'll gladly add a specific patch for that issue in #16737 after this one is committed.
comment:11 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | assigned → closed |
I've confirmed that the problem exists, but your proposed solution won't work (at least, not in the general case). type_ is the name of a Python builtin, so you shouldn't name a model field
type
. An alternate approach would be to append or prepend a known character (e.g., xtype, type_1) to create a field name.