33

I currently added this model to my app

from mainApp.models import modelPatient

class modelBodyParts(models.Model):
    part_name             = models.CharField(max_length=1000, unique=False , default="")
    modelPatient          = models.ForeignKey(modelPatient)
    result                = models.CharField(max_length=3000, unique=False , default="")

Now the makemigrations and migrate commands give me the following error

 >>python manage.py makemigrations 
 >>python ./manage.py migrate

ValueError: The field interviewApp.modelInterviewAnswers.patients was declared with a lazy reference to 'mainApp.modelpatients', but app 'mainApp' doesn't provide model 'modelpatients'

I am not sure what that means. But I do remember that at one point mainApp.modelpatients existed and then it was changed to mainApp.modelpatient which still exists.How do I resolve this issue and why is this showing up ? Any help would be appreciated.

James Franco
  • 4,516
  • 10
  • 38
  • 80

5 Answers5

13

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,821
  • 18
  • 23
  • 2
    I ran into @JamesFranco's same error when trying to undo a `ForeignKey` that was added across apps, and your solution helped. The automatic migration by Django hadn't listed the second app in `dependencies`, so I had to manually add it – Addison Klinke Nov 14 '19 at 23:33
  • flix, it's wrote me raise CircularDependencyError( django.db.migrations.exceptions.CircularDependencyError – Sergo Jun 25 '22 at 08:41
5

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

mehmet
  • 7,720
  • 5
  • 42
  • 48
Udi
  • 29,222
  • 9
  • 96
  • 129
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
  • 8,322
  • 2
  • 47
  • 70
-2

Just delete the previous migration files, once you make new changes to your models to inherit from each other , it's best to delete the older migrations made then do a fresh 'makemigrations'

-3

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

Mr Coolman
  • 29
  • 4