在Django中,当试图转储数据时,会得到一个“错误:无法序列化数据库”?

时间:2021-10-26 15:37:52

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:

在我的live服务器上,当我试图将数据转储到Djanog 1.2.1中的JSON fixture时,会出现错误。在live服务器上,它运行的是MySQL服务器版本5.0.77,我使用phpMyAdmin接口向表导入了大量数据。网站运行良好,Django管理员回复正常。但是当我尝试将对应于表的应用程序的数据转储出来时,我就会得到这个错误:

$ 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:

我的Django模型用于“gigs”,我正试图从这些模型中删除它们。py文件:

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:

正如我所说,Django对数据没有问题。该网站运行良好,关系似乎运作良好。运行命令获取SQL Django正在使用的内容:

$ 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.

我对数据进行了三重检查,以确保导入后所有的关系和数据都是正确的。但我还是犯了这个错误,三天之后……我不知道该怎么办。我无法想象“不赞成警告”在这里会成为一个问题。我真的需要把这些数据作为JSON转储出来。

Many thanks for any help at all.

非常感谢你的帮助。

2 个解决方案

#1


11  

Could be something similar to this.

可能是类似的东西。

Run it with:

运行该程序:

python manage.py dumpdata --indent=2 -v 2 --traceback gigs 

To see the underlying error.

查看潜在的错误。

#2


3  

I once ran in a similar problem where the error message was as mesmerizing as yours. The cause was a lack of memory on my server. It seems that generating dumps in json is quite memory expensive. I had only 60meg of memory (at djangohosting.ch) and it was not enough to get a dump for a mysql DB for which the mysql dump was only 1meg.

我曾经遇到过类似的问题,错误消息和您的一样令人着迷。原因是我的服务器内存不足。在json中生成转储文件似乎非常昂贵。我只有60meg的内存(在djangohost.ch),而对于mysql DB来说,转储仅仅是1meg是不够的。

I was able to find out by watching the python process hit the 60meg limit using the top command in a second command line while running manage.py dumpdata in a first one.

我可以通过在运行manage时使用第二个命令行中的top命令来观察python进程达到60meg限制来发现这一点。第一个是py dumpdata。

My solution : get the mysql dump and then load it on my desktop pc, before generating the json dump. That said, for backup purposes, the mysql dumps are enough.

我的解决方案是:获取mysql转储,然后将其加载到我的桌面pc上,然后生成json转储。也就是说,出于备份目的,mysql转储就足够了。

The command to get a mysql dump is the following :

获取mysql转储的命令如下:

mysqldump -p [password] -u [username] [database_name] > [dump_file_name].sql

That said, your problem could be completely different. You should really look at every table that has a foreign key to your Location table, and check if there is no field pointing to a previously deleted location. Unfortunately MySQL is very bad at maintaining Referential integrity, and you cannot count on it.

也就是说,你的问题可能完全不同。您应该真正地查看包含位置表外键的每个表,并检查是否没有字段指向先前删除的位置。不幸的是,MySQL在维护引用完整性方面非常糟糕,您不能指望它。

#1


11  

Could be something similar to this.

可能是类似的东西。

Run it with:

运行该程序:

python manage.py dumpdata --indent=2 -v 2 --traceback gigs 

To see the underlying error.

查看潜在的错误。

#2


3  

I once ran in a similar problem where the error message was as mesmerizing as yours. The cause was a lack of memory on my server. It seems that generating dumps in json is quite memory expensive. I had only 60meg of memory (at djangohosting.ch) and it was not enough to get a dump for a mysql DB for which the mysql dump was only 1meg.

我曾经遇到过类似的问题,错误消息和您的一样令人着迷。原因是我的服务器内存不足。在json中生成转储文件似乎非常昂贵。我只有60meg的内存(在djangohost.ch),而对于mysql DB来说,转储仅仅是1meg是不够的。

I was able to find out by watching the python process hit the 60meg limit using the top command in a second command line while running manage.py dumpdata in a first one.

我可以通过在运行manage时使用第二个命令行中的top命令来观察python进程达到60meg限制来发现这一点。第一个是py dumpdata。

My solution : get the mysql dump and then load it on my desktop pc, before generating the json dump. That said, for backup purposes, the mysql dumps are enough.

我的解决方案是:获取mysql转储,然后将其加载到我的桌面pc上,然后生成json转储。也就是说,出于备份目的,mysql转储就足够了。

The command to get a mysql dump is the following :

获取mysql转储的命令如下:

mysqldump -p [password] -u [username] [database_name] > [dump_file_name].sql

That said, your problem could be completely different. You should really look at every table that has a foreign key to your Location table, and check if there is no field pointing to a previously deleted location. Unfortunately MySQL is very bad at maintaining Referential integrity, and you cannot count on it.

也就是说,你的问题可能完全不同。您应该真正地查看包含位置表外键的每个表,并检查是否没有字段指向先前删除的位置。不幸的是,MySQL在维护引用完整性方面非常糟糕,您不能指望它。