Krystian Cybulski
Krystian Cybulski

Reputation: 11118

Failing fixture load: DoesNotExist: ... matching query does not exist

Running Django 1.5.x from git repo. Using south to manage migrations. I have a model such as this:

class Company(models.Model):
    name = models.CharField(max_length = 100)
    subdomain = models.CharField(max_length = 50)
    is_active = models.BooleanField(default=True)
    prefs = models.TextField(max_length=8*1024, null=True, blank=True, default=None)

    is_dev = models.BooleanField(default=False) # this is a development company - internal to 15Five

    trial_start_date = models.DateField(null=True, blank=True, default=None)
    trial_length_days = models.PositiveIntegerField(default = 28)

    email_domain = models.CharField(max_length = 100, null=True, blank=True, default=None)
    cohort = models.ForeignKey(Cohort, null=True, blank=True, default=None)

    create_ts = models.DateTimeField(_('Created'), default=timezone.now)
    update_ts = models.DateTimeField(_('Updated'), default=timezone.now)
    deactivate_ts = models.DateTimeField(_('Deactivated'), null=True, blank=True, default=None)
    converted_to_paying_ts = models.DateTimeField(_('Converted'), null=True, blank=True, default=None)

    features = models.ManyToManyField(Feature,
                                      verbose_name=_('feature'), blank=True,
                                      help_text='A feature which can be turned on or off for a company')

I created a company in the django admin. I used manage.py dumpdata ff.company --indent=2 to export a JSON fixture:

[
{
  "pk": 1,
  "model": "ff.company",
  "fields": {
    "cohort": null,
    "subdomain": "zzz",
    "name": "zzz",
    "trial_start_date": null,
    "trial_length_days": 0,
    "converted_to_paying_ts": "2012-12-02T11:06:10Z",
    "is_active": true,
    "update_ts": "2012-12-02T11:06:11Z",
    "is_dev": true,
    "deactivate_ts": null,
    "create_ts": "2012-12-02T11:05:56Z",
    "email_domain": "zzz.com",
    "prefs": "",
    "features": []
  }
}
]

I clear out my ff_company table and run my migrations and I get an error:

Error in migration: ff:0004_create_default_companies
DoesNotExist: Problem installing fixture 'C:\<redacted>/migrations/fixtures/create_default_companies.json': Company matching query does not exist. Lookup parameters were {'id': 1}

Any idea what the issue could be? I think south is not the issue, as I get the error as well if I load the fixture manually:

>manage.py loaddata ..\migrations\fixtures\create_default_companies.json
DoesNotExist: Problem installing fixture 'C:\<redacted>\migrations\fixtures\create_default_companies.json': Company matching query does not exist. Lookup parameters were {'id': 1}

Upvotes: 3

Views: 4070

Answers (2)

gtlee
gtlee

Reputation: 116

When fixture files are processed, the data is saved to the database as is. Model defined save() methods are not called, and any pre_save or post_save signals will be called with raw=True since the instance only contains attributes that are local to the model what-s-a-fixture.

from django.db.models.signals import post_save
from .models import MyModel

def my_handler(**kwargs):
    # disable the handler during fixture loading
    if kwargs['raw']:
        return
    ...

post_save.connect(my_handler, sender=MyModel)

Upvotes: 1

Krystian Cybulski
Krystian Cybulski

Reputation: 11118

I had pre_save and post_save signal triggers on my Company model. These were not checking the raw param and were trying to do some smart things on database values which did not exist.

Upvotes: 16

Related Questions