Opened 8 years ago

Last modified 7 years ago

#28313 closed Cleanup/optimization

Model name length is not enforced or validated — at Initial Version

Reported by: Michal Dabski Owned by: nobody
Component: contrib.contenttypes Version: 1.11
Severity: Normal Keywords: contenttype, model, name, length, migration
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When creating a model with name longer than 100 characters, migrate will fail with the following trace:

Traceback (most recent call last):
  File "C:/Users/Mick/Desktop/Projects/django/meg-forms/manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line
    utility.execute()
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\core\management\__init__.py", line 355, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\core\management\base.py", line 330, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\core\management\commands\migrate.py", line 227, in handle
    self.verbosity, self.interactive, connection.alias, apps=post_migrate_apps, plan=plan,
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\core\management\sql.py", line 53, in emit_post_migrate_signal
    **kwargs
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\dispatch\dispatcher.py", line 193, in send
    for receiver in self._live_receivers(sender)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\contrib\auth\management\__init__.py", line 63, in create_permissions
    ctype = ContentType.objects.db_manager(using).get_for_model(klass)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\contrib\contenttypes\models.py", line 60, in get_for_model
    model=opts.model_name,
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\query.py", line 466, in get_or_create
    return self._create_object_from_params(lookup, params)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\query.py", line 498, in _create_object_from_params
    obj = self.create(**params)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\query.py", line 394, in create
    obj.save(force_insert=True, using=self.db)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\base.py", line 806, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\base.py", line 836, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\base.py", line 922, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\base.py", line 961, in _do_insert
    using=using, raw=raw)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\query.py", line 1063, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\models\sql\compiler.py", line 1099, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\backends\utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\Mick\Desktop\Projects\django\meg-forms\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(100)

saving sqlmigrate to a file and executing it will work fine, which makes it harder to determine what the issue is - the schema migration does not have any inserts.
I found that the issue was caused by model name length not fitting into ContentType's 100 character limit.

I suggest any of the following
# Validate model names in ContentType model and raise a validation error so it's more clear to the user what is causing it
# Enforce model name length limit
# Truncate model name in ContentType objects down to 100 characters

Change History (0)

Note: See TracTickets for help on using tickets.
Back to Top