Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django issue during migrations - lazy reference

Tags:

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.

like image 840
James Franco Avatar asked Mar 04 '17 09:03

James Franco


People also ask

How do I fix migration issues in Django?

you can either: temporarily remove your migration, execute python manage.py migrate, add again your migration and re-execute python manage.py migrate. Use this case if the migrations refer to different models and you want different migration files for each one of them.

What is the difference between Makemigrations and migrate in Django?

migrate , which is responsible for applying and unapplying migrations. makemigrations , which is responsible for creating new migrations based on the changes you have made to your models.

What does fake migration do in Django?

To deal with such cases, Django provides a way called fake migration, which applies the migration but does not affect the database schema. In simpler words, for each migration which is faked, an entry is created in django_migrations table but no changes are done in the database schema.

What happens if I delete all migrations Django?

Deleting Django migrations is generally a bad idea. Django keeps track of what's in your db through these migration files, as well as through a table it creates in your db, and if you delete any of this Django will start throwing errors on migrate that can be hard to fix.


2 Answers

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
    ]
like image 167
flix Avatar answered Sep 17 '22 11:09

flix


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

like image 36
Udi Avatar answered Sep 19 '22 11:09

Udi