Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#11936 closed (fixed)

Proxy models' names are too long for django.contrib.auth which causes weird test failures

Reported by: ryszard Owned by: clamothe
Component: Database layer (models, ORM) Version: 1.1
Severity: Keywords: test, only, deferred fields
Cc: ryszard.szopa+django@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


When you call SomeModel.objects.only('somefield'), then the generated deferred proxy model class name may be too long for django.contrib.auth.models.Permission, resulting in DB warnings which are raised as an exception.

This manifests itself if you use only in your tests cases. The just created proxy model will still be floating among the normal models and will try to create Permission objects, resulting in queries like

INSERT INTO auth_permission (name, content_type_id, codename) VALUES (Can add episode_ deferred_air_date_art_large_art_medium_art_small_created_description_duration_free_available_free_channel_free_video_url_info_modified_number_parental_rating_released_season_streaming_available_subscription_id, 19, add_episode_deferred_air_date_art_large_art_medium_art_small_created_description_duration_free_available_free_channel_free_video_url_info_modified_number_parental_rating_released_season_streaming_available_subscription_id)

which will cause the NEXT test case to fail.

I was able to reproduce this on Linux, with Python 2.6, MySQL_python 1.2.3c1 and MySQL 5.0.51a (it may depend on how django reacts to MySQL warnings). I will attach a and that allow to reproduce this bug.

Potential solutions: django.db.models.get_models should not return proxies for deferred models, the deferred proxy models should have shorter names or their type some custom repr.

Attachments (4) (906 bytes) - added by ryszard 7 years ago.
models file to trigger the bug (498 bytes) - added by ryszard 7 years ago.
tests to trigger the bug
fix_r11689.diff (2.4 KB) - added by clamothe 7 years ago.
Fix and regression test against r11689
fix_r11901.diff (2.3 KB) - added by clamothe 7 years ago.
Fix and regression test against r11901

Download all attachments as: .zip

Change History (12)

Changed 7 years ago by ryszard

models file to trigger the bug

Changed 7 years ago by ryszard

tests to trigger the bug

comment:1 Changed 7 years ago by ryszard

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

comment:2 Changed 7 years ago by ryszard

  • Cc ryszard.szopa+django@… added

comment:3 Changed 7 years ago by kmtracey

Reposting previously posted non-formatted traceback. PLEASE use preview and make sure things are readable before pressing "submit changes."

This had been posted by ryszard:

The error that results from runnign the tests with the attached models

(note that test_bbb fails, even though it contains only a pass

 (ryszard hacksaw):~/setjam/setjam% ./ test pathology
 Creating test database...
 Creating table pathology_pathologicallylongname
 Creating table auth_permission
 Creating table auth_group
 Creating table auth_user
 Creating table auth_message
 Creating table django_content_type
 Creating table django_session
 Creating table django_site
 Creating table django_admin_log
 Creating table django_flatpage
 Installing index for auth.Permission model
 Installing index for auth.Message model
 Installing index for admin.LogEntry model
 Installing index for flatpages.FlatPage model


 ERROR: test_bbb (pathology.tests.SimpleTest)
 Traceback (most recent call last):
  File "/home/ryszard/setjam/site-python/django/test/", line
 242, in __call__
  File "/home/ryszard/setjam/site-python/django/test/", line
 217, in _pre_setup
  File "/home/ryszard/setjam/site-python/django/test/", line
 440, in _fixture_setup
    return super(TestCase, self)._fixture_setup()
  File "/home/ryszard/setjam/site-python/django/test/", line
 222, in _fixture_setup
    call_command('flush', verbosity=0, interactive=False)
  File "/home/ryszard/setjam/site-
 python/django/core/management/", line 166, in call_command
    return klass.execute(*args, **defaults)
  File "/home/ryszard/setjam/site-python/django/core/management/",
 line 222, in execute
    output = self.handle(*args, **options)
  File "/home/ryszard/setjam/site-python/django/core/management/",
 line 351, in handle
    return self.handle_noargs(**options)
  File "/home/ryszard/setjam/site-
 python/django/core/management/commands/", line 61, in
    emit_post_sync_signal(models.get_models(), verbosity, interactive)
  File "/home/ryszard/setjam/site-python/django/core/management/",
 line 205, in emit_post_sync_signal
  File "/home/ryszard/setjam/site-python/django/dispatch/",
 line 166, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/home/ryszard/setjam/site-
 python/django/contrib/auth/management/", line 28, in
    defaults={'name': name, 'content_type': ctype})
  File "/home/ryszard/setjam/site-python/django/db/models/",
 line 123, in get_or_create
    return self.get_query_set().get_or_create(**kwargs)
  File "/home/ryszard/setjam/site-python/django/db/models/", line
 335, in get_or_create
  File "/home/ryszard/setjam/site-python/django/db/models/", line
 410, in save
    self.save_base(force_insert=force_insert, force_update=force_update)
  File "/home/ryszard/setjam/site-python/django/db/models/", line
 495, in save_base
    result = manager._insert(values, return_id=update_pk)
  File "/home/ryszard/setjam/site-python/django/db/models/",
 line 177, in _insert
    return insert_query(self.model, values, **kwargs)
  File "/home/ryszard/setjam/site-python/django/db/models/", line
 1087, in insert_query
    return query.execute_sql(return_id)
  File "/home/ryszard/setjam/site-
 python/django/db/models/sql/", line 320, in execute_sql
    cursor = super(InsertQuery, self).execute_sql(None)
  File "/home/ryszard/setjam/site-python/django/db/models/sql/",
 line 2369, in execute_sql
    cursor.execute(sql, params)
  File "/home/ryszard/setjam/site-
 python/django/db/backends/mysql/", line 84, in execute
    return self.cursor.execute(query, args)
  File "/home/buildbot/bbenv/lib/python2.6/site-
 line 175, in execute
    if not self._defer_warnings: self._warning_check()
  File "/home/buildbot/bbenv/lib/python2.6/site-
 line 89, in _warning_check
    warn(w[-1], self.Warning, 3)
 Warning: Data truncated for column 'name' at row 1

 Ran 1 test in 0.621s

 FAILED (errors=1)
 Destroying test database...

comment:4 Changed 7 years ago by clamothe

  • Has patch set
  • Owner changed from nobody to clamothe

I've attached a patch that solves the problem for me. The patch applies cleanly against r11689, includes a regression test, and does not break the existing test suite. The included regression test fails if the patch to is not applied.

With the patch, the django.db.models.loading.get_models method will no longer return any deferred models.

The patch doesn't change get_model, so the deferred models can still be accessed via the app cache if they're individually looked up (as with

Changed 7 years ago by clamothe

Fix and regression test against r11689

comment:5 Changed 7 years ago by b1tr0t

clamothe: Are you sure that's the correct patch you added? At first glance it doesn't look related to the problem, and certainly it doesn't fix the bug for me on 1.2 pre-alpha. :)

comment:6 Changed 7 years ago by clamothe

Yes, I added the correct patch. I will explain how it fixes the problem for me.

The problem

If the following sounds like gibberish then read the background info first.

In the aftermath of a query that used QuerySet's defer() or only() methods, a deferred model could exist. Without my patch, the results of the django.db.models.loading.get_models method include deferred models. With my patch, the results of this method do not include deferred models.

If deferred models are included in the results of this method then they could be mistaken for real models. An example of of such is when django.contrib.auth autopopulates it's permission table. Certainly creating separate permissions for deferred models is not intended. Additionally, the name of the deferred model's class might not fit in the 50 characters allowed in Permission's name field, causing a database error or warning such as:

 Warning: Data truncated for column 'name' at row 1

Background information

When QuerySet's defer() or only() methods are used in a query, django dynamically creates a model that includes only the fields that the QuerySet needs. If User.objects.only('username', 'password') is called then django will create a special deferred User model that only includes the username and password fields. Django would call this model User_Deferred_username_password. If a model has many fields and one or two fields are deferred in a query then the name of the deferred model created for the queryset can be very long, as it includes the names of each field that will be retrieved.

Keep that in mind while we jump over to django.contrib.auth. This app creates the permissions for each installed model whenever the database is reset or syncdb is called. Remember the model permissions that you can assign to Users and Groups when using the admin? This is where those permissions are created. The method that creates these permissions first looks up each installed model, using django.db.models.loading.get_models to get the installed models, then creates the Permission objects for each installed model. Each Permission object is assigned a name. The name field contains a description of the permission (i.e. "Can add Group") and has a max_length of 50.

Changed 7 years ago by clamothe

Fix and regression test against r11901

comment:7 Changed 7 years ago by russellm

  • Resolution set to fixed
  • Status changed from new to closed

(In [11938]) Fixed #11936 -- Removed deferred models from the list returned by the app_cache. Thanks to ryszard for the report, and clamothe for the initial patch.

comment:8 Changed 7 years ago by russellm

(In [11942]) [1.1.X] Fixed #11936 -- Removed deferred models from the list returned by the app_cache. Thanks to ryszard for the report, and clamothe for the initial patch.

Backport of r11938 from trunk.

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