#28084: FieldDoesNotExist when running a migration on Django 1.11
-----------------------------------------+------------------------
               Reporter:  Bas ten Berge  |          Owner:  nobody
                   Type:  Bug            |         Status:  new
              Component:  Uncategorized  |        Version:  1.11
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 Hi,

 I'm running into an issue when performing this change to this model:


 {{{
 class TopicOwnerThoughtStatus(models.Model):
     '''
     Registers topic owner stats

     '''
     thought = models.ForeignKey(Thought, help_text='Associates the state
 to a thought', db_index=True)
     user = models.ForeignKey(settings.AUTH_USER_MODEL,
 related_name='topicownerstatus_set', help_text='Associates this state to
 an user')
     topic = models.ForeignKey(Topic, help_text='Associates this state to a
 thought and topic')
     notified_at = models.DateTimeField(null=True, blank=True,
 help_text='Contains the timestamp the topicowner was notified')
     shown_at = models.DateTimeField(null=True, blank=True,
 help_text='Contains the timestamp the topicowner viewed the thoughts')
     finished_at = models.DateTimeField(null=True, blank=True,
 help_text='Contains the timestamp the topicowner marked the topic closed')

     class Meta:
         unique_together = ('thought', 'user', 'topic', )
         ordering = ('thought', )
         verbose_name_plural = 'Topic owner status'
 }}}

 Changing this model to:
 {{{
 class TopicOwnerThoughtStatus(models.Model):
     '''
     Registers topic owner stats

     '''
     thought = models.ForeignKey(Thought, help_text='Associates the state
 to a thought', db_index=True)
     user = models.ForeignKey(settings.AUTH_USER_MODEL,
 related_name='topicownerstatus_set', help_text='Associates this state to
 an user')
     topic_selector = models.CharField(max_length=36, null=True,
 blank=True, help_text='Contains the topic selector')
     notified_at = models.DateTimeField(null=True, blank=True,
 help_text='Contains the timestamp the topicowner was notified')
     shown_at = models.DateTimeField(null=True, blank=True,
 help_text='Contains the timestamp the topicowner viewed the thoughts')
     finished_at = models.DateTimeField(null=True, blank=True,
 help_text='Contains the timestamp the topicowner marked the topic closed')

     class Meta:
         unique_together = ('thought', 'user', 'topic_selector', )
         ordering = ('thought', )
         verbose_name_plural = 'Topic owner status'

 }}}

 will allow me to create a migration, but I'm not able to run that
 migration:
 {{{
 (env-brownpapersession)bastb@bastb-
 vps:/var/www/brownpapersession/dev/brownpapersession$ python manage.py
 migrate                                            Operations to perform:
   Apply all migrations: ..., topic
 Running migrations:
   Applying topic.0014_auto_20170417_0941...Traceback (most recent call
 last):
   File "manage.py", line 10, in <module>
     execute_from_command_line(sys.argv)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/core/management/__init__.py", line 363, in
 execute_from_command_line
     utility.execute()
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/core/management/__init__.py", line 355, in execute
     self.fetch_command(subcommand).run_from_argv(self.argv)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/core/management/base.py", line 283, in run_from_argv
     self.execute(*args, **cmd_options)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/core/management/base.py", line 330, in execute
     output = self.handle(*args, **options)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/core/management/commands/migrate.py", line 204, in handle
     fake_initial=fake_initial,
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 115, in migrate
     state = self._migrate_all_forwards(state, plan, full_plan, fake=fake,
 fake_initial=fake_initial)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 145, in
 _migrate_all_forwards
     state = self.apply_migration(state, migration, fake=fake,
 fake_initial=fake_initial)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/migrations/executor.py", line 244, in apply_migration
     state = migration.apply(state, schema_editor)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/migrations/migration.py", line 129, in apply
     operation.database_forwards(self.app_label, schema_editor, old_state,
 project_state)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/migrations/operations/models.py", line 536, in
 database_forwards
     getattr(new_model._meta, self.option_name, set()),
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/backends/base/schema.py", line 348, in
 alter_unique_together
     self._delete_composed_index(model, fields, {'unique': True},
 self.sql_delete_unique)
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/backends/base/schema.py", line 371, in
 _delete_composed_index
     columns = [model._meta.get_field(field).column for field in fields]
   File "/var/www/brownpapersession/dev/env-
 brownpapersession/local/lib/python2.7/site-
 packages/django/db/models/options.py", line 619, in get_field
     raise FieldDoesNotExist("%s has no field named '%s'" %
 (self.object_name, field_name))
 }}}

 I'm able to run this migration when following a two-step approach:
 * add the topic_selector field
 * change the unique_together to read ('thought', 'user',
 'thought_selector', )
 * create and run the migration
 * remove the topic field
 * create and run the migration

 I'd expect to run this in one migration, though.

--
Ticket URL: <https://code.djangoproject.com/ticket/28084>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.4f316947b56b91f5bad10181249b12d7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to