error - no such table

I’m trying to make an e-mail authorization and get an error when creating superuser. What’s wrong?

application ‘www’
models.py

from django.contrib.auth.models import AbstractBaseUser, BaseUserManager, PermissionsMixin
from django.db import models
from django.utils import timezone

class UserManager(BaseUserManager):

def _create_user(self, email, password, is_staff, is_superuser, **extra_fields):
if not email:
raise ValueError(‘Users must have an email address’)
now = timezone.now()
email = self.normalize_email(email)
user = self.model(
email=email,
is_staff=is_staff,
is_active=True,
is_superuser=is_superuser,
last_login=now,
date_joined=now,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
return user

def create_user(self, email, password, **extra_fields):
return self._create_user(email, password, False, False, **extra_fields)

def create_superuser(self, email, password, **extra_fields):
user=self._create_user(email, password, True, True, **extra_fields)
return user

class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(max_length=254, unique=True)
name = models.CharField(max_length=254, null=True, blank=True)
is_staff = models.BooleanField(default=False)
is_superuser = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
last_login = models.DateTimeField(null=True, blank=True)
date_joined = models.DateTimeField(auto_now_add=True)

USERNAME_FIELD = 'email'
EMAIL_FIELD = 'email'
REQUIRED_FIELDS = []

objects = UserManager()

def get_absolute_url(self):
    return "/users/%i/" % (self.pk)

settings.py

INSTALLED_APPS = [
‘django.contrib.admin’,
‘django.contrib.auth’,
‘django.contrib.contenttypes’,
‘django.contrib.sessions’,
‘django.contrib.messages’,
‘django.contrib.staticfiles’,
‘www’,
]

ROOT_URLCONF = ‘www.urls’

AUTH_PASSWORD_VALIDATORS = [
{
‘NAME’: ‘django.contrib.auth.password_validation.UserAttributeSimilarityValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.MinimumLengthValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.CommonPasswordValidator’,
},
{
‘NAME’: ‘django.contrib.auth.password_validation.NumericPasswordValidator’,
},
]

AUTH_USER_MODEL = ‘www.User’

error

(env) isaev@web:~/plg/www$ python manage.py createsuperuser
Email: isaev@plg.ru
Traceback (most recent call last):
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py”, line 413, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: www_user

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File “manage.py”, line 22, in
main()
File “manage.py”, line 18, in main
execute_from_command_line(sys.argv)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/core/management/init.py”, line 401, in execute_from_command_line
utility.execute()
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/core/management/init.py”, line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/core/management/base.py”, line 330, in run_from_argv
self.execute(*args, **cmd_options)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py”, line 79, in execute
return super().execute(*args, **options)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/core/management/base.py”, line 371, in execute
output = self.handle(*args, options)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py”, line 113, in handle
error_msg = self._validate_username(username, verbose_field_name, database)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/contrib/auth/management/commands/createsuperuser.py”, line 234, in _validate_username
self.UserModel._default_manager.db_manager(database).get_by_natural_key(username)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/contrib/auth/base_user.py”, line 45, in get_by_natural_key
return self.get(
{self.model.USERNAME_FIELD: username})
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/models/manager.py”, line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/models/query.py”, line 425, in get
num = len(clone)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/models/query.py”, line 269, in len
self._fetch_all()
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/models/query.py”, line 1308, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/models/query.py”, line 53, in iter
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/models/sql/compiler.py”, line 1156, in execute_sql
cursor.execute(sql, params)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/utils.py”, line 98, in execute
return super().execute(sql, params)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/utils.py”, line 66, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/utils.py”, line 75, in _execute_with_wrappers
return executor(sql, params, many, context)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/utils.py”, line 90, in exit
raise dj_exc_value.with_traceback(traceback) from exc_value
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/utils.py”, line 84, in _execute
return self.cursor.execute(sql, params)
File “/home/isaev/plg/env/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py”, line 413, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: www_user

1 Like

Did you do a makemigrations followed by a migrate? Did you see the table being created as part of the migrate process? Can you go directly into the database and verify that the table exists?

Yes

(env) isaev@web:~/plg/www$ python manage.py makemigrations
No changes detected
(env) isaev@web:~/plg/www$ python manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial… OK
Applying admin.0001_initial… OK
Applying admin.0002_logentry_remove_auto_add… OK
Applying admin.0003_logentry_add_action_flag_choices… OK
Applying contenttypes.0002_remove_content_type_name… OK
Applying auth.0001_initial… OK
Applying auth.0002_alter_permission_name_max_length… OK
Applying auth.0003_alter_user_email_max_length… OK
Applying auth.0004_alter_user_username_opts… OK
Applying auth.0005_alter_user_last_login_null… OK
Applying auth.0006_require_contenttypes_0002… OK
Applying auth.0007_alter_validators_add_error_messages… OK
Applying auth.0008_alter_user_username_max_length… OK
Applying auth.0009_alter_user_last_name_max_length… OK
Applying auth.0010_alter_group_name_max_length… OK
Applying auth.0011_update_proxy_permissions… OK
Applying auth.0012_alter_user_first_name_max_length… OK
Applying sessions.0001_initial… OK

And I don’t see table ‘www_user’ in my database (

sqlite> .open db.sqlite3
sqlite> .tables
auth_group django_admin_log django_session
auth_group_permissions django_content_type
auth_permission django_migrations
sqlite>

So you can see that you don’t have a migration for app www - the easiest way to fix this is by specifying the app name on your makemigrations command:
manage.py makemigrations www

4 Likes

You’re a genius. Everything worked ) Thanks a lot

you helped me. thanks

Hi @KenWhitesell ,

I am getting same error on makemigrations call .

getting error as follow
django.db.utils.OperationalError: no such table: app_mynewmodel

And error is throwing from view.py at following point :-

if MyNewModel.objects.filter().exist():

If I remove the code line above, I can see that the migration is successful .It seems like issue with exist() call . Is this expected error ?

This is likely a different problem - please open a new topic to discuss it, along with posting the complete view causing this issue. Also, provide a brief description of what you had done up to this point. (Is this the first makemigrations for this project? Was it working before adding that line of code?)

I also have same problem, makemigration app_name ran successfully but the error still exists
please help me too. I deleted all previous migrations and ran makemigrations and migrate commands again but still the same problem

This is likely a different problem than the ones discussed previously - please open a new topic to discuss it

Hi,
i have been struggling with the same problem, could you please let me know if you were able to fix this. ‘manage.py makemigrations appname’ did not work for me

This is likely a different problem than the ones discussed previously - please open a new topic to discuss it.
Provide a brief description of your project and what you have done up to this point along with the relevant details of the error you’re encountering. (Is this the first makemigrations for this project? Did it ever work?)