gpt4 book ai didi

mysql - Django 使用 dumpdata 创建数据库备份

转载 作者:搜寻专家 更新时间:2023-10-30 20:43:52 25 4
gpt4 key购买 nike

我有一个生产服务器在运行,还有一个本地开发服务器(这个使用简单的运行服务器等)。

我计划使用 Django 内置的“dumpdata”命令,以便经常创建数据库备份。可悲的是,我似乎无法让“loaddata”接受“dumpdata”提供的内容。

在生产服务器上:

python manage.py dumpdata > db_backup.json

这给了我一个大约 6MB 的文件,然后我将其传输到我的本地机器。在我的本地机器上:

bash-3.2$ python manage.py flush --settings=config.settings_dev
/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/fields/subclassing.py:80: DeprecationWarning: A Field class whose get_db_prep_save method hasn't been updated to take a `connection` argument.
new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)
You have requested a flush of the database.
This will IRREVERSIBLY DESTROY all data currently in the 'dcif_db' database,
and return each table to the state it was in after syncdb.
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): no
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_manager(settings)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/__init__.py", line 438, in execute_manager
utility.execute()
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/__init__.py", line 379, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 191, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 220, in execute
output = self.handle(*args, **options)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/base.py", line 351, in handle
return self.handle_noargs(**options)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/commands/flush.py", line 74, in handle_noargs
emit_post_sync_signal(set(all_models), verbosity, interactive, db)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/sql.py", line 190, in emit_post_sync_signal
interactive=interactive, db=db)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/contrib/auth/management/__init__.py", line 51, in create_permissions
content_type=ctype
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/manager.py", line 138, in create
return self.get_query_set().create(**kwargs)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/query.py", line 360, in create
obj.save(force_insert=True, using=self.db)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/base.py", line 460, in save
self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/base.py", line 553, in save_base
result = manager._insert(values, return_id=update_pk, using=using)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/manager.py", line 195, in _insert
return insert_query(self.model, values, **kwargs)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/query.py", line 1436, in insert_query
return query.get_compiler(using=using).execute_sql(return_id)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 791, in execute_sql
cursor = super(SQLInsertCompiler, self).execute_sql(None)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 735, in execute_sql
cursor.execute(sql, params)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/backends/util.py", line 34, in execute
return self.cursor.execute(sql, params)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
return self.cursor.execute(query, args)
django.db.utils.DatabaseError: can't adapt type '__proxy__'

几周前我遇到过这个问题,只要再次运行任何产生错误的命令,错误就会消失。这不是理想的解决方案,但我不确定还有什么方法可以解决它。

然后:

bash-3.2$ python manage.py loaddata etc/db_backups/12_9_11.json --settings=config.settings_dev
/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/fields/subclassing.py:80: DeprecationWarning: A Field class whose get_db_prep_save method hasn't been updated to take a `connection` argument.
new_class = super(SubfieldBase, cls).__new__(cls, name, bases, attrs)
Problem installing fixture 'etc/db_backups/12_9_11.json': Traceback (most recent call last):
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/management/commands/loaddata.py", line 174, in handle
obj.save(using=using)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/core/serializers/base.py", line 165, in save
models.Model.save_base(self.object, using=using, raw=True)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/base.py", line 570, in save_base
created=(not record_exists), raw=raw, using=using)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/dispatch/dispatcher.py", line 172, in send
response = receiver(signal=self, sender=sender, **named)
File "/Applications/djangostack-1.3-0/python/lib/python2.6/site-packages/djangobb_for_dcif/signals.py", line 18, in post_saved
profile = post.user.forum_profile
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/fields/related.py", line 315, in __get__
rel_obj = QuerySet(self.field.rel.to).using(db).get(**params)
File "/Applications/djangostack-1.3-0/apps/django/lib/python2.6/site-packages/django/db/models/query.py", line 349, in get
% self.model._meta.object_name)
DoesNotExist: User matching query does not exist.

dumpdata 提供的 .json 文件不应该是一致的,并且没有像这个错误似乎声称的那样丢失对象吗?

在生产服务器上,我使用的是 MySQL,而我在本地使用的是 PostgreSQL,所以我不确定这是否是问题的原因。

有更简单的方法吗?

最佳答案

我认为最好的方法是将表导出为简单的 sql 插入语句然后你可以在目标数据库中重新运行插入 sql 脚本

我正在使用这种方法。我也尝试过 dumpdata 等,但插入语句的方式简单而不复杂。

关于mysql - Django 使用 dumpdata 创建数据库备份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8447426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com