5

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?

4
  • Try running python manage.py makemigrations --app-name before running python manage.py migrate// or check if you have faked the previous migrate process Commented Jan 7, 2015 at 8:28
  • You're working the the Test-Driven Design book by Harry Percival right? I'm running into the exact same problem. We must have missed a step somewhere. Commented Feb 15, 2015 at 6:57
  • Eric: I figured out a solution that worked for me. Turns out I didn't miss a step, but the place where he places his sql file is different from where Django naturally creates it. Django's base sqlite file is under src/db.sqlite3. However, if you look under src/superlists/db.sqlite3, you'll find that another sqlite file was created as per the path given by the settings for BASE_DIR. I suspect the discrepancy is a result of the book using an older version of Django Commented Feb 16, 2015 at 6:48
  • How do you serve your Django app? Are you sure you DJANGO_SETTINGS_MODULE are the same in both manage.py & web server? Commented Nov 24, 2015 at 8:29

1 Answer 1

2

@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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.