I'm using Django and I was having the error after attempting manual fixes on the migrations folder.:
ValueError: The field API_Materials.ModelParams.material_param was declared with a lazy reference to 'API_Materials.materialparams', but app 'API_Materials' doesn't provide model 'materialparams'.
The structure in models.py
for those two classes cited in the error mensage ("MaterialParams", and "ModelParams") is:
class MaterialParams(models.Model):
material = models.ForeignKey(Material, models.CASCADE, related_name='params')
name = models.CharField(max_length=50)
submitted_by = models.ForeignKey(User, models.SET_NULL, null=True, related_name='material_params')
class ModelParams(models.Model):
model = models.ForeignKey(Model, models.CASCADE, related_name='params')
material_param = models.ForeignKey(MaterialParams, models.CASCADE, related_name='model_params')
submitted_by = models.ForeignKey(User, models.SET_NULL, null=True, related_name='params')
params = models.JSONField() # {"x": 10, "z": 40, "output_do_outro" : 30} // {"input": [12, 1, 3.4], "output":}
class Meta:
constraints = [
models.UniqueConstraint(fields=['model', 'material_param'], name='unique_model_params')
]
As usual with migrations, I tried to comment those classes, create a makemigrations
, and then uncomment, but it didn't work. I also attempted to comment the "material_param" with the constraints in "ModelParams", but with the same result.
Looking for similar questions in Stack Overflow, usually they were related to the User or by the lack of "Model.models" heritance. Later I deleted the "ModelParams" table in the DB Server to attempt to remove this error, but without positive results.
At some point, I tried to delete the database to restart again with manage.py flush
, since the data wasn't that much. However, I noticed that in the DB Server the tables in the app still existed, but others didn't (such as users for login). But still, the tables related to the problem ("MaterialParams", and "ModelParams") don't exist anymore, so I don't know if the others can be related to it.
I also attempted to run manage.py migrate <app> zero
, but the command gives the same error and the migration fails.
How could I reset the DB (or migrations?) to avoid this error?
To fix the problem I needed to reset the migration by removing the migration files ("0001_initial, 0002_...) from the corresponding directory and then migrating by:
python manage.py makemigrations
python manage.py migrate
However, because I manually dropped both "ModelParams" and "MaterialParams" tables from the DB Server, it was necessary to comment these models in models.py
(and consequentially in any files that used these models) and executing migrate with --fake
to not apply it, since the tables don't exist anymore:
python manage.py makemigrations
python manage.py migrate --fake
Finally, I uncommented the models in all files and migrated again, without the --fake
flag.