hltcoe / turkle

Django-based clone of Amazon's Mechanical Turk service running in your local environment.
https://turkle.readthedocs.io
Other
147 stars 47 forks source link

Adding a field to the Project model breaks existing migrations #136

Closed charman closed 3 years ago

charman commented 3 years ago

Problem caused by current version of turkle/migrations/0006_copy_can_work_on_permission.py.

Apparently, it's not a good idea to write Django migrations that use the Django ORM wrapper to instantiate fully-hydrated model instances.

Steps to reproduce:

The test runner runs the migration suite while trying to build the test database, and the migration fails with an sqlite3 error before the test runner can even start the test suite:

Running migrations:
  Applying contenttypes.0001_initial... OK (0.003s)
[...]
  Applying turkle.0006_copy_can_work_on_permission...Traceback (most recent call last):
  File "/Users/charman/opt/anaconda3/envs/turkle/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/Users/charman/opt/anaconda3/envs/turkle/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 383, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such column: turkle_project.allotted_assignment_time

which the stack trace reveals is caused by Turkle migration 0006 - turkle/migrations/0006_copy_can_work_on_permission.py#L47:

for project in Project.objects.all():

This line in migration 0006 is causing Django to try to instantiate Project instances using all of the fields defined in turkle/models.py - but the new field that we added to turkle/models.py does not get added to the database until the new migration (numbered 0010 or higher, based on existing migrations) is run - and the new migration is never run because migration 0006 fails first.

cash commented 3 years ago

Could we use the only() method on the QuerySet object to only pull back the fields that we need to update permissions on the batches?