2

I've been trying to figure this out all day without any luck...

This is the error that I'm getting when I try to migrate an app after using the "makemigrations" command.

self._related_fields = self.resolve_related_fields()
File "/home/cg/webdev/rivos/local/lib/python2.7/site-packages/django-django-834d78f/django/db/models/fields/related.py", line 1386, in resolve_rel                                                                              ated_fields
raise ValueError('Related model %r cannot be resolved' % self.rel.to)
ValueError: Related model 'checkout_mgr.ReturnReceipt' cannot be resolved

This is the migration that seems to be creating the error:

# encoding: utf8
from django.db import models, migrations
import django.core.validators


class Migration(migrations.Migration):

dependencies = [
    ('checkout_mgr', '0001_initial'),
]

operations = [
    migrations.CreateModel(
        name='ReturnLineItem',
        fields=[
            (u'id', models.AutoField(verbose_name=u'ID', serialize=False, auto_created=True, primary_key=True)),
            ('return_receipt', models.ForeignKey(to='checkout_mgr.ReturnReceipt', to_field=u'id')),
            ('purchase_line_item', models.ForeignKey(to='checkout_mgr.ReceiptLineItem', to_field=u'id')),
            ('return_reason', models.CharField(default='', max_length=2, choices=[('', '-- Please Select --'), ('PD', 'Product defective'), ('PU', 'Product unsatisfactory'), ('CU', 'Customer unsatisfied'), ('LP', 'Customer found lower price'), ('CV', 'Competitor offers more value')])),
            ('return_reason_details', models.TextField(blank=True)),
            ('quantity', models.IntegerField(default=1, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(100)])),
        ],
        options={
        },
        bases=(models.Model,),
    ),
]

The related models:

class ReturnReceipt(Receipt):
class Meta:
    proxy = True

def __init__(self, *args, **kwargs):
    """
    Overload __init__ to set the receipt type to RF: Return receipt
    """
    super(ReturnReceipt, self).__init__(*args, **kwargs)
    self.type = 'RF'
    self._subtotal = self._tax_total = self._total = 0
    self.totals_calculated = False


class ReturnLineItem(models.Model):
    return_receipt = models.ForeignKey(ReturnReceipt)

ReturnReceipt is a proxy model and is being used as a Foreign Key in the ReturnLineItem model.

Django seems to generate the migration correctly above, but I don't understand the error.

I though I'd post here first rather than file a bug report in case I'm doing something completely silly.

Brownbay
  • 5,400
  • 3
  • 25
  • 30
  • Are `ReturnReceipt` and `ReturnLineItem` in the same App? If not I found being useful whenever this error occurs is to add `'other_app', '000X_migration_the_dependant_Model_was_introduced'` to the dependencies list of the migrations file. – Martin Grohmann Mar 31 '14 at 13:47
  • Yep, they're both in the same app. I was able to overcome the issue by referencing the actual non-proxy model in the ForeignKey statement. Eg. instead of saying models.ForeignKey(ReturnReceipt) where ReturnReceipt is a proxy model, I had to instead write models.Foreignkey(Receipt). This wasn't the case before Django 1.7. – Brownbay Apr 02 '14 at 00:55
  • I ended up having to downgrade back to Django 1.6 because I needed Foreign key support for proxy models :( – Ian weisberger Jan 20 '15 at 07:00

2 Answers2

0

I was through the same error a few days ago. I had some apps and some classes sharing the same name, for instance: app called person and a model called Person, when I run makemigrations and migrate the error mentioned above take place. The easiest way I found was made a short refactoring and rename my models, delete the old migrations and make new ones. Not so clever but I was running out of time. Still looks like a bug for me.

  • Good idea. I tried something similar, but kept running into issues. I was mid-way through a project with Django1.7 and because of how unstable the migrations feature is, I've scrapped Django and restarted again with Flask + SQLAlchemy (or Peewee ORM). Even with Django1.7 now released, if you check the bug-tracker, there are still a plethora of migration related bugs... – Brownbay Sep 25 '14 at 19:36
0

The solution which worked for me is to delete my migrations folder and database completely thereafter running command-

python manage.py makemigrations

python manage.py migrate

because this error occured to me due to some misplacement of foreign key, and even after undo , this error was not going.

sean zhao
  • 1
  • 1