[Django]-Django issue during migrations – lazy reference

5πŸ‘

βœ…

Try using RenameModel and RenameField. See answer here: https://stackoverflow.com/a/26241640/57952

πŸ‘€Udi

15πŸ‘

For me, this error occurred, because I was swapping a ForeignKey Model from

my_field = models.ForeignKey('old.model', ...)

to

my_field = models.ForeignKey('new.model', ...)

The solution was to edit the resulting migration manually, and add the latest migration from the new app as a dependency:

class Migration(migrations.Migration):
    dependencies = [
        ('old', '0016_whatever'),
        ('new', '0002_latest_migration'),   # Add this line
    ]
πŸ‘€flix

1πŸ‘

I just had this again on the latest django. A shared model was renamed (but not moved to a new app), and the issue was that it failed to pick up one of the other apps that was referencing it.

I can’t see why it had an issue with just one app but not the others. The initial makemigraitons went fine, so it just needed to be added to the migration file it created. After that, subsequent calls to makemigrations return "no changes detected", as it should be.

# Generated by Django 3.2.5
from django.db import migrations

class Migration(migrations.Migration):
    dependencies = [
        # all the following apps were correctly added as dependencies
        ('supplier',  '0001_initial'),
        ('marketing', '0004_add_some_field'),
        ('devops',    '0002_fix_some_model'),
        ('sales',     '0001_initial'),
        ('warehouse', '0010_set_trained'),
        ('products',  '0005_change_sku_format'),
        ('search',    '0003_remove_bad_data'),

        # Manually add this one, for the app that was missed
        ('left_out',  '0007_change_name'),
    ]

    operations = [
        migrations.RenameModel(
            old_name='OldModelName',
            new_name='NewModelName',
        ),
    ]

πŸ‘€Andrew

-5πŸ‘

delete the entire migration folder and then create it again with the __init__.py file and thank me later.

πŸ‘€Mr Coolman

Leave a comment