Opened 4 years ago

Closed 2 years ago

#19422 closed Bug (wontfix)

Model subclass not in INSTALLED_APPS incorrectly collected during object deletion

Reported by: gcc Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


For example, if there is a Session subclass somewhere in your code, but not in an INSTALLED_APPS, then the following code will fail:

s = Session()
from datetime import datetime
s.expire_date =

It fails here, when trying to delete objects:

Traceback (most recent call last):
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/project/demo/", line 14, in test_demo
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/", line 575, in delete
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/", line 175, in collect
    if not sub_objs:
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/", line 130, in __nonzero__
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/", line 118, in _result_iter
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/", line 875, in _fill_cache
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/", line 291, in iterator
    for row in compiler.results_iter():
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/sql/", line 763, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/models/sql/", line 818, in execute_sql
    cursor.execute(sql, params)
  File "/home/installuser/Dropbox/projects/ischool/delete_related_test/ve/local/lib/python2.7/site-packages/django/db/backends/sqlite3/", line 337, in execute
    return Database.Cursor.execute(self, query, params)
DatabaseError: no such table: utility_sessionwithextrafield

It seems that the model was registered just by being loaded, and associated itself with a parent model (superclass), but its database table never got created because it wasn't in INSTALLED_APPS.

So these two should be consistent with each other: either we only register models for INSTALLED_APPS, or we create database tables for all registered models even if they're not in INSTALLED_APPS.

Test case repro code is at:

Change History (2)

comment:1 Changed 4 years ago by ptone

  • Component changed from Uncategorized to Database layer (models, ORM)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Summary changed from Model subclass not in INSTALLED_APPS breaks object deletion to Model subclass not in INSTALLED_APPS incorrectly collected during object deletion
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug
  • Version changed from 1.4 to master

Looks like the deletion collector is incorrectly attempting to collect related multi-table models that are not installed - my guess is that a good strategy for a fix would be to use the installed flag on the model meta


which should probably happen in options get_all_related_objects

Not sure there is any internal use-case for getting related models that are not installed - but this would need some quick review of what all uses that method.

comment:2 Changed 2 years ago by aaugustin

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

After the app loading refactor, you aren't allowed to import models that aren't in an installed application.

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