简体   繁体   English

Django-来自JSON转储的loaddata给出了'no such table'错误

[英]Django - loaddata from JSON dump gives 'no such table' error

I am trying to load a JSON dump of my Django Sqlite db into a fresh db, using manage.py loaddata . 我正在尝试使用manage.py loaddata Django Sqlite数据库的JSON转储加载到新的数据库中。 I am encountering the following django.db.utils.OperationalError , however: 但是,我遇到以下django.db.utils.OperationalError

Could not load foo.Bar(pk=1): no such table: foo_bar

The table is definitely present in the database, and all my migrations are applied, checked using showmigrations . 该表肯定存在于数据库中,并且使用showmigrations检查并应用了我的所有迁移。 So I am stumped as to what's going on. 所以我对发生的事情感到困惑。 I would not be surprised if it's something simple, however. 但是,如果它很简单,我不会感到惊讶。 Would appreciate any suggestions. 将不胜感激任何建议。

NB : The db I'm trying to load the data into is just an empty file. 注意 :我试图将数据加载到的数据库只是一个空文件。 Might that be the issue? 可能是问题所在吗? Do I need to create the relevant tables in that fresh db before loading into it from the fixture? 从夹具加载到新数据库中之前,我需要创建相关表吗?

If you are still in development, try to reset the database. 如果您仍在开发中,请尝试重置数据库。 Be aware that all your data will get lost. 请注意,所有数据都会丢失。 Create and dump some new data and check if you still get the same error. 创建并转储一些新数据,并检查是否仍然出现相同的错误。

Here is a script how to reset the database: 这是一个脚本,如何重置数据库:

#!/usr/bin/env bash

#deletes the sqlite3 database
rm db.sqlite3

#deletes migration files of your module(s)
# __init__.py needs to be recreated
rm -R your_module/migrations/*
touch your_module/migrations/__init__.py

# migrates changes and synchronizes the database
python manage.py makemigrations && python manage.py migrate
python manage.py migrate --fake-initial
python manage.py migrate --run-syncdb

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM