Django OperationalError: missing table; migration does not recognize missing table

别说谁变了你拦得住时间么 提交于 2019-12-22 08:24:05

问题


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

However, at

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

I get an Error message :

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

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

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. 

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?


回答1:


@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'),
    }
}

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


来源:https://stackoverflow.com/questions/27814144/django-operationalerror-missing-table-migration-does-not-recognize-missing-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!