Error on execute migration (create index) when set db_name with quoted string

185 views
Skip to first unread message

Carlos Leite

unread,
Nov 27, 2017, 11:01:56 PM11/27/17
to Django users
I was making some introspections on meta attributes from a Model class
jsut to check what changes when we set some attributes on Meta class and etc...


TO check the Meta.db_name
I read the docs and saw that I could use quoted strings as told ..


"
... o prevent such transformations, use a quoted name as the value for db_table:

> db_table = '"name_left_in_lowercase"'

 Such quoted names can also be used with Django’s other supported database backends; except for Oracle, however, the quotes have no effect. See the Oracle notes for more details.
"
at https://docs.djangoproject.com/en/1.8/ref/models/options/#db-table


Well, when I tried to *migrate* I got the error, during the index creation, described below.
Is it a bug ? or I miss soething ?
I just tried to set a custom name for a table, with quotes and hyphens 8P


'"big_name-with-hyphen-left_in_lowercase"'

the error hapends when PostgreSQL tries to create an index and Django named with part of the tables name. 


### The Model Class


class Publisher(models.Model):
"""
Book's Author - author is a Book's model supplement.
"""
name = models.CharField(verbose_name='publisher name', max_length=50, null=False)


class Meta:
db_table = '"big_name-with-hyphen-left_in_lowercase"'
get_latest_by = "name"
ordering = ['name', ]
verbose_name = 'Publiser'
verbose_name_plural = 'Publishers'
indexes = [
models.Index(fields=['name', ]),
]




### The Migration 0007


# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-11-28 03:15
from __future__ import unicode_literals


from django.db import migrations, models




class Migration(migrations.Migration):


dependencies = [
('testapp', '0006_auto_20171127_1927'),
]


operations = [
migrations.RemoveIndex(
model_name='publisher',
name='testapp_pub_name_88e073_idx',
),
migrations.AddIndex(
model_name='publisher',
index=models.Index(fields=['name'], name='"big_name-w_name_cd0539_idx'),
),
migrations.AlterModelTable(
name='publisher',
table='"big_name-with-hyphen-left_in_lowercase"',
),
]




### traceback


$python manage.py makemigrations
System check identified some issues:


Migrations for 'testapp':
testproject/testapp/migrations/0007_auto_20171128_0315.py
- Remove index testapp_pub_name_88e073_idx from publisher
- Create index "big_name-w_name_cd0539_idx on field(s) name of model publisher
- Rename table for publisher to "big_name-with-hyphen-left_in_lowercase"
(dj_datadictionary) 20171125.Sat01:15:52cadu>/Volumes/p10G/prj/dj_datadictionary_testproject/testproject>




$python manage.py migrate
System check identified some issues:


Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions, testapp
Running migrations:
Applying testapp.0007_auto_20171128_0315...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/core/management/__init__.py", line 363, in execute_from_command_line
utility.execute()
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/core/management/__init__.py", line 355, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/migrations/migration.py", line 129, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 785, in database_forwards
schema_editor.add_index(model, self.index)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 330, in add_index
self.execute(index.create_sql(model, self))
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/backends/base/schema.py", line 119, in execute
cursor.execute(sql, params)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/backends/utils.py", line 80, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Users/cadu/Envs/dj_datadictionary/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: zero-length delimited identifier at or near """"
LINE 1: CREATE INDEX ""big_name-w_name_cd0539_idx" ON "testapp_publi...
^

Carlos Leite

unread,
Nov 27, 2017, 11:24:59 PM11/27/17
to Django users
ooops 


in the migration 0007 the index name seems badly formed 

```python 
   ...
    migrations.AddIndex(
    model_name='publisher',
    index=models.Index(fields=['name'], name='"big_name-w_name_cd0539_idx'),   # <<<<<<<  there is a " in plus. and its never closed. 
),
```

Simon Charette

unread,
Nov 28, 2017, 1:14:51 AM11/28/17
to Django users
Hey Carlos,

I believe the trailing quote truncation issue might be solved in the yet to be released
1.11.8[0][1] version.

Could you confirm whether or not it's the case? You'll have to regenerate your migration.

Best,
Simon

[0] https://github.com/django/django/commit/a35ab95ed4eec5c62fa19bdc69ecfe0eff3e1fca
[1] https://code.djangoproject.com/ticket/28792

Carlos Leite

unread,
Nov 28, 2017, 6:27:16 AM11/28/17
to Django INT
Thanks Simon, 

I tryied to find some issue related but found nothing. 

The ticket #28792 seems exactly the case ...  

I'll take a couple of hours (at work), but I'll try that patch 
and regenarate the migrations. 

thanks again. 

---------------------------------------------------------------------
Cadu Leite
| Twitter     | Medium Blog | Google +     |  
| @cadu_leite | @cadu_leite+CarlosLeite |


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/059d8feb-45dd-4f30-aa7f-cfc3a71659d5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Carlos Leite

unread,
Nov 28, 2017, 8:54:23 AM11/28/17
to Django INT
Can I use double quoted names with others SGDBs , PostgreSQL ? 
or it is specific for Oracle ...

by the way the patch didint work. Its another problem Is not a metter of "." namespaced name.
 Its about if I can or not use '"tb_custom_name"'  double quoted string  = ' " " '

if I can use that (' " " ') for any database engine, I believe its a bug ...  
and even changed the migration manually, the same error occur when create an INDEX (at my example) ... 

so, the error is at the method that generates the db_name string passed for the ORM to create tables and constraints. 




---------------------------------------------------------------------
Cadu Leite
| Twitter     | Medium Blog | Google +     |  
| @cadu_leite | @cadu_leite+CarlosLeite |


Carlos Leite

unread,
Nov 28, 2017, 9:01:06 AM11/28/17
to Django INT

Testing the pacth for ticket/28792 at [1]
with Python 2.7, Django 1.11
to test badly formed  double quoted db_table name

    db_table = '"tbl_litle_name"',

[1] https://code.djangoproject.com/ticket/28792



Django Branch 1.11
------------------

I've aplied this commit

$pip install -e git+https://github.com/django/django.git@a35ab95ed#egg=django



Migration Initial
-----------------


# -*- coding: utf-8 -*-
# Generated by Django 1.11.8.dev20171115030630 on 2017-11-28 13:44

from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Author',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, verbose_name='author name')),
            ],
            options={
                'ordering': ['name'],
                'get_latest_by': 'name',
                'verbose_name': 'Author',
                'verbose_name_plural': 'Authors',
            },
        ),
        migrations.CreateModel(
            name='Book',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(help_text="book's name", max_length=50, verbose_name='book name')),
                ('isbn_10', models.CharField(blank=True, db_column='old_isbn_number', db_index=True, help_text='international standart book number (old length 10)', max_length=10, null=True)),
                ('isbn_13', models.CharField(blank=True, db_index=True, help_text='international standart book number (after 2007 jan 01)', max_length=13, unique=True)),
                ('language', models.CharField(blank=True, choices=[('PORTUGUESE_BRAZIL', 'Portugu\xeas Brasil'), ('PORTUGUES', 'Portugu\xeas'), ('ENGLISH', 'English'), ('FRANCE', 'France'), ('ESPANISH', 'Espa\xf1ol')], help_text='edition length', max_length=5, null=True)),
            ],
            options={
                'ordering': ['name'],
                'get_latest_by': 'name',
                'verbose_name': 'book',
                'verbose_name_plural': 'books',
            },
        ),
        migrations.CreateModel(
            name='Publisher',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, verbose_name='publisher name')),
            ],
            options={
                'get_latest_by': 'name',
                'ordering': ['name'],
                'verbose_name_plural': 'Publishers',
                'db_table': '"tbl_litle_name"',
                'verbose_name': 'Publiser',

            },
        ),
        migrations.AddIndex(
            model_name='publisher',
            index=models.Index(fields=['name'], name='"tbl_litle__name_408be5_idx'),
        ),
        migrations.AddField(
            model_name='book',
            name='author',
            field=models.ManyToManyField(blank=True, help_text="author's name", null=True, to='testapp.Author'),
        ),
        migrations.AddField(
            model_name='book',
            name='publisher',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to='testapp.Publisher'),
        ),
        migrations.AddIndex(
            model_name='author',
            index=models.Index(fields=['name'], name='testapp_aut_name_02c4bc_idx'),
        ),
    ]



The model changed
-----------------


class Publisher(models.Model):
    """
    Book's Author - author is a Book's model supplement.
    """
    name = models.CharField(verbose_name='publisher name', max_length=50, null=False)

    class Meta:
        db_table = '"tbl_litle_name"'

        get_latest_by = "name"
        ordering = ['name', ]
        verbose_name = 'Publiser'
        verbose_name_plural = 'Publishers'
        indexes = [
            models.Index(fields=['name', ]),
        ]

    def __unicode__(self):
        return self.name


Generated Migration
-------------------


# -*- coding: utf-8 -*-
# Generated by Django 1.11.8.dev20171115030630 on 2017-11-28 13:36

from __future__ import unicode_literals

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

     #  ...  deleted for sanity

        migrations.CreateModel(
            name='Publisher',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('name', models.CharField(max_length=50, verbose_name='publisher name')),
            ],
            options={
                'get_latest_by': 'name',
                'ordering': ['name'],
                'verbose_name_plural': 'Publishers',
                'db_table': '"big_name-with-hyphen-left_in_lowercase"',
                'verbose_name': 'Publiser',

            },
        ),
        migrations.AddIndex(
            model_name='publisher',
            index=models.Index(fields=['name'], name='"big_name-w_name_cd0539_idx'), # <=====  error `"`
        ),

       #  code deleted for sanity
    ]


MakeMigration ERROR  & traceback
--------------------------------

(dj28792) 20171128.Tue11:40:32cadu>/Volumes/p10G/prj/dj_datadictionary_testproject/testproject>
cadu.[563]$python manage.py migrate

System check identified some issues:

WARNINGS:
testapp.Book.author: (fields.W340) null has no effect on ManyToManyField.

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK
(dj28792) 20171128.Tue11:44:50cadu>/Volumes/p10G/prj/dj_datadictionary_testproject/testproject>
cadu.[564]$python manage.py makemigrations

System check identified some issues:

WARNINGS:
testapp.Book.author: (fields.W340) null has no effect on ManyToManyField.
Migrations for 'testapp':
  testproject/testapp/migrations/0001_initial.py
    - Create model Author
    - Create model Book
    - Create model Publisher
    - Create index "tbl_litle__name_408be5_idx on field(s) name of model publisher   <==========
    - Add field author to book
    - Add field publisher to book
    - Create index testapp_aut_name_02c4bc_idx on field(s) name of model author
(dj28792) 20171128.Tue11:44:55cadu>/Volumes/p10G/prj/dj_datadictionary_testproject/testproject>
cadu.[565]$python manage.py migrate

System check identified some issues:

WARNINGS:
testapp.Book.author: (fields.W340) null has no effect on ManyToManyField.

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, testapp
Running migrations:
  Applying testapp.0001_initial...Traceback (most recent call last):

  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/executor.py", line 115, in migrate

    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/executor.py", line 145, in _migrate_all_forwards

    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/migration.py", line 129, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/operations/models.py", line 788, in database_forwards
    schema_editor.add_index(model, self.index)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/base/schema.py", line 331, in add_index
    self.execute(index.create_sql(model, self))
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/utils.py", line 64, in execute

    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: zero-length delimited identifier at or near """"
LINE 1: CREATE INDEX ""tbl_litle__name_408be5_idx" ON "tbl_litle_nam...  <=======================
                     ^

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
even editing the migration manually  
 ...  the problem appears again on created CONTRAINT for a PK
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

(dj28792) 20171128.Tue11:45:02cadu>/Volumes/p10G/prj/dj_datadictionary_testproject/testproject>
cadu.[566]$python manage.py migrate

System check identified some issues:

WARNINGS:
testapp.Book.author: (fields.W340) null has no effect on ManyToManyField.

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, testapp
Running migrations:
  Applying testapp.0001_initial...Traceback (most recent call last):

  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/__init__.py", line 364, in execute_from_command_line
    utility.execute()
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/__init__.py", line 356, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "/Users/cadu/Envs/dj28792/src/django/django/core/management/commands/migrate.py", line 204, in handle
    fake_initial=fake_initial,
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/executor.py", line 115, in migrate

    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/executor.py", line 145, in _migrate_all_forwards

    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/base/schema.py", line 93, in __exit__
    self.execute(sql)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/base/schema.py", line 120, in execute
    cursor.execute(sql, params)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/cadu/Envs/dj28792/src/django/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: syntax error at or near "tbl_litle_name"
LINE 1: ...ONSTRAINT "testapp_book_publisher_id_fb33c7ca_fk_"tbl_litle_...
                                                             ^
                     ^


---------------------------------------------------------------------
Cadu Leite
| Twitter     | Medium Blog | Google +     |  
| @cadu_leite | @cadu_leite | +CarlosLeite |

http://people.python.org.br/

>>> To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Simon Charette

unread,
Nov 28, 2017, 3:30:31 PM11/28/17
to Django users
Hello Carlos,

Thank you for taking the time to reproduce the issue against the upcoming
1.11 release.

It looks like `Index` class doesn't use the same name generation logic as
the schema editor for some reason[0] so it will be have to be adjusted in
a similar way.

Could you file a new ticket[1] referencing this thread and detailing the issue
you are encoutering? It should be eligible for a backport since this is a bug
in a newly introduced feature.

Until a 1.11.x version fixing the issue is released you can work around the
issue by defining an Index(name)[2] manually.

Cheers,
Simon

[0] https://github.com/django/django/blob/899999db4293d40613626833860de28e8ccdd413/django/db/models/indexes.py#L96-L122
[1] https://code.djangoproject.com/newticket
[2] https://docs.djangoproject.com/en/1.11/ref/models/indexes/#name

Carlos Leite

unread,
Nov 29, 2017, 1:01:41 PM11/29/17
to Django INT

Sure, 
its great to help the project. 
thanks for your attention. 

tonight (brazil) I will prepare the ticket. 

cheers


---------------------------------------------------------------------
Cadu Leite
| Twitter     | Medium Blog | Google +     |  
| @cadu_leite | @cadu_leite+CarlosLeite |


To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Carlos Leite

unread,
Dec 2, 2017, 2:04:55 PM12/2/17
to Django INT
Thanks again Simon



---------------------------------------------------------------------
Cadu Leite
| Twitter     | Medium Blog | Google +     |  
| @cadu_leite | @cadu_leite+CarlosLeite |


Reply all
Reply to author
Forward
0 new messages