在 Django 中,收到“错误:无法序列化数据库”当尝试转储数据时?

发布于 2024-09-19 02:33:28 字数 2946 浏览 2 评论 0原文

当我尝试将数据转储到实时服务器上 Djanog 1.2.1 中的 JSON 固定装置时,出现错误。在实时服务器上,它运行 MySQL Server 版本 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.

我试图转储的“演出”的 Django 模型在 models.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)

就像我说的,Django 对数据没问题。该网站运行良好,而且关系似乎运行得非常好。当运行命令来获取 Django 正在使用的 SQL 时:

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

我已经对数据进行了三次检查,以确保导入后所有关系和数据都正常。但三天后我仍然收到此错误...我不知道该怎么办。我无法想象“DeprecationWarning”会成为这里的问题。我确实需要将这些数据转储为 JSON。

非常感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

蒲公英的约定 2024-09-26 02:33:29

可能类似于这个

运行它:

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

以查看潜在的错误。

Could be something similar to this.

Run it with:

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

To see the underlying error.

无远思近则忧 2024-09-26 02:33:29

我曾经遇到过类似的问题,其中的错误消息和你的一样令人着迷。原因是我的服务器内存不足。看来以 json 格式生成转储相当耗费内存。我只有 60meg 内存(在 djangohosting.ch),并且不足以获取 mysql 数据库的转储,而 mysql 转储只有 1meg。

我通过在第二个命令行中使用 top 命令观察 python 进程达到 60meg 限制,同时在第一个命令行中运行 manage.py dumpdata 来找到答案。

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

获取 mysql 转储的命令如下:

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

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

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.

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.

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.

The command to get a mysql dump is the following :

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.

纸伞微斜 2024-09-26 02:33:29

你可以——排除那个造成问题的特定应用程序,仍然会有数据库表,它对我有用

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

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
十年不长 2024-09-26 02:33:29

显示此错误是因为您的数据库架构和模型之间不匹配。

您可以尝试手动查找它,也可以直接安装 django-extensions

pip install django-extensions

并使用 sqldiff 命令 将打印出问题的确切位置。

python manage.py sqldiff -a -t

首先也是最重要的,使您的模型与您的数据库相匹配。然后运行迁移和假迁移:

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

仅此一项就可以让您运行转储。一旦 django 确保数据库的模式与您的模型相匹配,它就会让您这样做。

接下来,您可以像往常一样更新模型并重新运行迁移:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文