#28612 closed Bug (duplicate)
inspectdb sets max_length to -1 if no field length specified in the DB schema
| Reported by: | rvernica | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.11 |
| Severity: | Normal | Keywords: | |
| Cc: | Mariusz Felisiak | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Postgres schema:
# \d instance
Table "public.instance"
Column | Type | Modifiers
--------------------+-----------------------------+-----------
instance_id | bigint | not null
membership_id | bigint | default 0
host | character varying |
port | integer |
online_since | timestamp without time zone |
base_path | character varying |
server_id | integer |
server_instance_id | integer |
Indexes:
"instance_pkey" PRIMARY KEY, btree (instance_id)
"instance_host_port_key" UNIQUE CONSTRAINT, btree (host, port)
"instance_host_server_id_server_instance_id_key" UNIQUE CONSTRAINT, btree (host, server_id, server_instance_id)
"instance_server_id_server_instance_id_key" UNIQUE CONSTRAINT, btree (server_id, server_instance_id)
Check constraints:
"instance_base_path_non_unique" CHECK (check_base_path(base_path) = 0)
Notice the type for host and base_path attributes.
inspectdb output:
# python3 manage.py inspectdb instance # This is an auto-generated Django model module. # You'll have to do the following manually to clean this up: # * Rearrange models' order # * Make sure each model has one field with primary_key=True # * Make sure each ForeignKey has `on_delete` set to the desired behavior. # * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table # Feel free to rename the models, but don't rename db_table values or field names. from __future__ import unicode_literals from django.db import models class Instance(models.Model): instance_id = models.BigIntegerField(primary_key=True) membership_id = models.BigIntegerField(blank=True, null=True) host = models.CharField(max_length=-1, blank=True, null=True) port = models.IntegerField(blank=True, null=True) online_since = models.DateTimeField(blank=True, null=True) base_path = models.CharField(max_length=-1, blank=True, null=True) server_id = models.IntegerField(blank=True, null=True) server_instance_id = models.IntegerField(blank=True, null=True) class Meta: managed = False db_table = 'instance' unique_together = (('host', 'port'), ('server_id', 'server_instance_id'), ('host', 'server_id', 'server_instance_id'),)
Notice the max_length=-1 for host and base_path attributes.
The django.contrib.postgres app is listed in INSTALLED_APPS
PostgreSQL 9.6.5
Python 3.6.1
Django 1.11.5
Change History (5)
comment:1 by , 8 years ago
| Cc: | added |
|---|
comment:2 by , 8 years ago
comment:3 by , 8 years ago
Agreed. It should be closed as a duplicate. IMO the only possible solution is to treat them as a TextField's (at least on PostgreSQL) but this is not exactly the same.
comment:4 by , 8 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
comment:5 by , 8 years ago
If we're ready to accept the current situation -- where inspectdb produces output which cannot be used without changes, until #14094 is fixed -- then I think we can define the expected behavior as producing a CharField with max_length=None or no max_length at all. Leaving this open may have some value then: I suspect that, when taken on its own, this is an "easy pickings" ticket, and there's little motivation for experienced contributors to fix it.
Until Django supports
CharFieldwithmax_length=None(#14094), I'm not sure what the expected behavior would be. Can we close this as a duplicate of that ticket or do you think we should make some other change in the meantime?