Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #28056 - Reverse migration for model rename with cross-app ForeignKey fails #8488

Closed
wants to merge 1 commit into from

Conversation

gaurav8297
Copy link

@gaurav8297 gaurav8297 commented May 11, 2017

During the renaming of model. To maintain the migration state of related field it is necessary to include AlterField migration.

Ticket: https://code.djangoproject.com/ticket/28056

@timgraham
Copy link
Member

Did you test with the sample project on the ticket? After regenerating the migration and migrating forwards, I'm seeing this error:

Running migrations:
  Applying one.0001_initial... OK
  Applying one.0002_auto_20170616_1919... OK
  Applying two.0001_initial...Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/home/tim/code/django/django/core/management/__init__.py", line 354, in execute_from_command_line
    utility.execute()
  File "/home/tim/code/django/django/core/management/__init__.py", line 348, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/tim/code/django/django/core/management/base.py", line 280, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/tim/code/django/django/core/management/base.py", line 327, in execute
    output = self.handle(*args, **options)
  File "/home/tim/code/django/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/home/tim/code/django/django/db/migrations/executor.py", line 113, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/tim/code/django/django/db/migrations/executor.py", line 143, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/tim/code/django/django/db/migrations/executor.py", line 240, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/tim/code/django/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/tim/code/django/django/db/migrations/operations/models.py", line 92, in database_forwards
    schema_editor.create_model(model)
  File "/home/tim/code/django/django/db/backends/base/schema.py", line 242, in create_model
    definition, extra_params = self.column_sql(model, field)
  File "/home/tim/code/django/django/db/backends/base/schema.py", line 134, in column_sql
    db_params = field.db_parameters(connection=self.connection)
  File "/home/tim/code/django/django/db/models/fields/related.py", line 934, in db_parameters
    return {"type": self.db_type(connection), "check": self.db_check(connection)}
  File "/home/tim/code/django/django/db/models/fields/related.py", line 931, in db_type
    return self.target_field.rel_db_type(connection=connection)
  File "/home/tim/code/django/django/db/models/fields/related.py", line 852, in target_field
    return self.foreign_related_fields[0]
  File "/home/tim/code/django/django/db/models/fields/related.py", line 624, in foreign_related_fields
    return tuple(rhs_field for lhs_field, rhs_field in self.related_fields if rhs_field)
  File "/home/tim/code/django/django/db/models/fields/related.py", line 611, in related_fields
    self._related_fields = self.resolve_related_fields()
  File "/home/tim/code/django/django/db/models/fields/related.py", line 596, in resolve_related_fields
    raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
ValueError: Related model 'one.Rename1' cannot be resolved

As I mentioned on the ticket, I think the problem is in ForeignKey.db_type(). It uses self.target_field which is using the state of the current model files rather than migration's state. I'm not sure if this is fixable at the auto-detector level. Any insight @andrewgodwin?

@gaurav8297
Copy link
Author

@timgraham sorry for that when i tested i did migration and migrating first and after renaming model i did these two steps again and it worked for me. But now i think i have to check it again. I will try to resolve this and get back to you soon.

@timgraham
Copy link
Member

I guess I'll close it for now.

@timgraham timgraham closed this Jul 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants