Django OperationalError:缺少表;迁移不能识别丢失表 [英] Django OperationalError: missing table; migration does not recognize missing table

查看:262
本文介绍了Django OperationalError:缺少表;迁移不能识别丢失表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Django 1.7中遇到问题,我试图将用户保存到表,但我收到一个错误,表不存在。

I'm having trouble in Django 1.7, I am trying to save a user to a table, but I'm getting an error that the table does not exist.

这是我正在执行的代码:

Here is the code I'm executing:

from django.conf import settings
from django.contrib.auth import BACKEND_SESSION_KEY, SESSION_KEY, get_user_model
User = get_user_model()
from django.contrib.sessions.backends.db import SessionStore
from django.core.management.base import BaseCommand
class Command(BaseCommand):

    def handle(self, email, *_, **__):

        session_key = create_pre_authenticated_session(email)
        self.stdout.write(session_key)


def create_pre_authenticated_session(email):
    user = User.objects.create(email=email)
    session = SessionStore()
    session[SESSION_KEY] = user.pk
    session[BACKEND_SESSION_KEY] = settings.AUTHENTICATION_BACKENDS[0]
    session.save()
    return session.session_key

    user = User.objects.create(email=email)  

我收到错误讯息:

 django.db.utils.OperationalError: no such table: accounts_user  

这是我试图使用的accounts / models.py中的用户模型构建表:

Here is the user model at accounts/models.py that I'm trying to use to build the table:

from django.db import models
from django.utils import timezone

class User(models.Model):
    email = models.EmailField(primary_key=True)
    last_login = models.DateTimeField(default=timezone.now)
    REQUIRED_FIELDS = ()
    USERNAME_FIELD = 'email'

    def is_authenticated(self):
        return True


b $ b

我使用 'manage.py accounts 0001.initial' 对此迁移执行了sqlmigrate,我已经得到了正确的创建表SQL,但运行 'manage.py migrate' 可以提供以下信息:

I've run sqlmigrate against this migration with 'manage.py accounts 0001.initial' and I have gotten the correct create table SQL back, but running 'manage.py migrate' gives me the following :

Operations to perform:
  Apply all migrations: sessions, admin, lists, contenttypes, accounts, auth
Running migrations:
  No migrations to apply. 

迁移只是从shell运行makemigration,没有自定义代码的结果。我确实看到在所包括的应用程序列出的帐户,但迁移没有被运行,所以我的网站是在一个奇怪的地方,Django说当我尝试使用它时表缺少,但Django说,它存在,当我尝试运行迁移以创建它。
为什么Django错误地认为表已经存在,当我可以看看数据库,并看到它不?

The migration is just the result of running 'makemigration' from the shell, no custom code. I do see accounts listed in the included applications, but the migration isn't being ran, so my site is in an odd spot where Django says the table is missing when I try to use it, but Django says it exists when I try to run the migration to create it.
Why does Django erroneously think that the table already exists when I can look at the database and see that it doesn't?

推荐答案

@ user856358您对其他sqlite文件的评论似乎是根本原因。我遇到同样的错误,它是通过删除该文件并运行另一个迁移来解决。在我的示例中,文件位于 settings.py 中指定的位置:

@user856358 Your comment about the other sqlite file seems like the root cause. I encountered the same error, and it was resolved by removing that file and running another migration. In my case, the file was located as specified in settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, '../database/db.sqlite3'),
    }
}



删除.sqlite3文件在那里,我能够成功运行迁移并解决无表示错误...

By removing the .sqlite3 file there, I was able to successfully run the migration and resolve the no-such-table error...

django.db.utils.OperationalError: no such table: accounts_user
$ rm ../database/db.sqlite3 
$ python3 manage.py migrate

这篇关于Django OperationalError:缺少表;迁移不能识别丢失表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆