In Django, getting a "Error: Unable to serialize database" when trying to dump data?

Viewed 22587

I'm getting an error when I'm trying to dump data to a JSON fixture in Djanog 1.2.1 on my live server. On the live server it's running MySQL Server version 5.0.77 and I imported a lot of data to my tables using the phpMyAdmin interface. The website works fine and Django admin responds as normal. But when I try and actually dump the data of the application that corresponds to the tables I get this error:

$ python manage.py dumpdata --indent=2 gigs > fixtures/gigs_100914.json 
/usr/local/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
  from sets import ImmutableSet
Error: Unable to serialize database: Location matching query does not exist.

My Django model for 'gigs' that I'm trying to dump from looks like this in the models.py file:

from datetime import datetime
from django.db import models

class Location(models.Model):
    name = models.CharField(max_length=120, blank=True, null=True)

    class Meta:
        ordering = ['name']

    def __unicode__(self):
        return "%s (%s)" % (self.name, self.pk)

class Venue(models.Model):
    name = models.CharField(max_length=120, blank=True, null=True)
    contact = models.CharField(max_length=250, blank=True, null=True)
    url = models.URLField(max_length=60, verify_exists=False, blank=True, null=True) # because of single thread problems, I left this off (http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.URLField.verify_exists)

    class Meta:
        ordering = ['name']

    def __unicode__(self):
        return "%s (%s)" % (self.name, self.pk)

class Gig(models.Model):
    date = models.DateField(blank=True, null=True)
    details = models.CharField(max_length=250, blank=True, null=True)
    location = models.ForeignKey(Location)
    venue = models.ForeignKey(Venue)

    class Meta:
        get_latest_by = 'date'
        ordering = ['-date']

    def __unicode__(self):
        return u"%s on %s at %s" % (self.location.name, self.date, self.venue.name)

Like I say, Django is fine with the data. The site works fine and the relationships seem to operate absolutely fine. When a run the command to get what SQL Django is using:

$ python manage.py sql gigs
/usr/local/lib/python2.6/site-packages/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
  from sets import ImmutableSet
BEGIN;CREATE TABLE `gigs_location` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(120)
)
;
CREATE TABLE `gigs_venue` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(120),
    `contact` varchar(250),
    `url` varchar(60)
)
;
CREATE TABLE `gigs_gig` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `date` date,
    `details` varchar(250),
    `location_id` integer NOT NULL,
    `venue_id` integer NOT NULL
)
;
ALTER TABLE `gigs_gig` ADD CONSTRAINT `venue_id_refs_id_3d901b6d` FOREIGN KEY (`venue_id`) REFERENCES `gigs_venue` (`id`);
ALTER TABLE `gigs_gig` ADD CONSTRAINT `location_id_refs_id_2f8d7a0` FOREIGN KEY (`location_id`) REFERENCES `gigs_location` (`id`);COMMIT;

I've triple checked the data, gone through to make sure all the relationships and data is ok after importing. But I'm still getting this error, three days on... I'm stuck with what to do about it. I can't imagine the "DeprecationWarning" is going to be a problem here. I really need to dump this data back out as JSON.

Many thanks for any help at all.

4 Answers

you can --exclude that particular app which is creating problem , still there will be database tables , it worked for me

python manage.py dumpdata > backedup_data.json --exclude app_name

This error shows because there's a mismatch between your DB's schema and your Models.

You can try find it manually or you could just go ahead and install django-extensions

pip install django-extensions

and use the sqldiff command which will print you exactly wheres the problem.

python manage.py sqldiff -a -t

First and foremost, make your models match what your db has. Then run migrations and a fake migrate:

python manage.py makemigrations && python manage.py migrate --fake

That alone should let you run a dump. As soon as django makes sure the DB's schema matches your models, it will let you.

Moving forward, you can update your models and re-run the migrations as usual:

python manage.py makemigrations && python manage.py migrate
Related