Mysql exception when db sync with django manage.py script

I am relatively new to django. I defined my db schema and checked it without errors (manage.py checks error reports 0).

But when I run. /manage.py syncdb

I get the following stack trace:

Creating table demo_foobar_one Creating table demo_foobar_two <snip>...</snip> Traceback (most recent call last): File "manage.py", line 11, in <module> execute_manager(settings) File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 438, in execute_manager utility.execute() File "/usr/local/lib/python2.6/dist-packages/django/core/management/__init__.py", line 379, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 191, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 218, in execute output = self.handle(*args, **options) File "/usr/local/lib/python2.6/dist-packages/django/core/management/base.py", line 347, in handle return self.handle_noargs(**options) File "/usr/local/lib/python2.6/dist-packages/django/core/management/commands/syncdb.py", line 103, in handle_noargs emit_post_sync_signal(created_models, verbosity, interactive, db) File "/usr/local/lib/python2.6/dist-packages/django/core/management/sql.py", line 185, in emit_post_sync_signal interactive=interactive, db=db) File "/usr/local/lib/python2.6/dist-packages/django/dispatch/dispatcher.py", line 162, in send response = receiver(signal=self, sender=sender, **named) File "/usr/local/lib/python2.6/dist-packages/django/contrib/auth/management/__init__.py", line 28, in create_permissions defaults={'name': name, 'content_type': ctype}) File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 135, in get_or_create return self.get_query_set().get_or_create(**kwargs) File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 373, in get_or_create obj.save(force_insert=True, using=self.db) File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 435, in save self.save_base(using=using, force_insert=force_insert, force_update=force_update) File "/usr/local/lib/python2.6/dist-packages/django/db/models/base.py", line 528, in save_base result = manager._insert(values, return_id=update_pk, using=using) File "/usr/local/lib/python2.6/dist-packages/django/db/models/manager.py", line 195, in _insert return insert_query(self.model, values, **kwargs) File "/usr/local/lib/python2.6/dist-packages/django/db/models/query.py", line 1479, in insert_query return query.get_compiler(using=using).execute_sql(return_id) File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 783, in execute_sql cursor = super(SQLInsertCompiler, self).execute_sql(None) File "/usr/local/lib/python2.6/dist-packages/django/db/models/sql/compiler.py", line 727, in execute_sql cursor.execute(sql, params) File "/usr/local/lib/python2.6/dist-packages/django/db/backends/util.py", line 15, in execute return self.cursor.execute(sql, params) File "/usr/local/lib/python2.6/dist-packages/django/db/backends/mysql/base.py", line 86, in execute return self.cursor.execute(query, args) File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 176, in execute if not self._defer_warnings: self._warning_check() File "/usr/local/lib/python2.6/dist-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 92, in _warning_check warn(w[-1], self.Warning, 3) _mysql_exceptions.Warning: Data truncated for column 'name' at row 1 

I checked (and double checked) my table. All the name fields are CharField type with maximum length = 64. The db backend I'm using is MySQL, so I'm sure that indexes can be created for rows of length 64.

What could be causing this error (I suspect this is a misleading error message, even if it comes from db itself) ...

+7
django django-models
source share
3 answers

Tracking occurs during the creation of django.contrib.auth.Permission : some of them are automatically created for your models as part of syncdb .

Permission.name has max_length=50 , so you probably have an application class and / or model with a really long name?

Try the following query in manage.py dbshell :

 SELECT * FROM auth_permission WHERE LENGTH(name) = 50; 

If you cannot change the name of your model, you can fix this problem by reducing the length of the generated Permission.name by specifying verbose_name in the Meta model class (see here for more details):

 class MyVeryLongModelNameThatIsBreakingMyMigration(models.Model): class Meta: verbose_name = 'my long model' 

Update

There has been open (since 2013) a Django ticket to fix this:

+5
source share

As Piet Delport noted, the problem is that your model name is too big .

You will certainly have to shorten your model name and then clear the database. How you do this depends on where you are in the development process.

  • If this is a brand new application, with a dedicated database and no actual data, the simple answer is to drop and recreate the database and then re-run python manage.py syncdb .

  • If you have other tables in the database that you need to leave alone, but the tables for your Django do not have "real" data and can be omitted without damage, then you can use manage.py sqlclear to generate SQL DDL to delete all created Django tables, constraints and indexes.

    Follow these steps:

     apps="auth contenttypes sessions sites messages admin <myapp1> <myapp2>" python manage.py sqlclear ${apps} > clear.sql 

    You can submit the generated script to mysql or python manage.py dbshell . After that, you can re-run python manage.py syncdb .

  • If you have actual data in the database tables that cannot be deleted or deleted: Use and repeat 100 times "I will never do development against the production database again. I will always back up my databases before changing them. Now you will need to manually assemble the SQL, to change the name of the affected table, and all that refers to it, and any links in the table auth_permissions and any other system tables Django. your actual steps will depend entirely on the state Asha databases and tables.

+3
source share

I also got an error like this using postgresql django 1.2, but the problem was not long, but using ugettext_lazy to translate the names. ('can_purge', _("Can purge")) is obviously unacceptable because the name is stored in the database as text

+1
source share

All Articles