0

I am trying to build a custom user model which I had did successfully in a previous project. But for some reason this is time I am met with this weird error, which I have gone through stackoverflow and find a fix for this. I try to do makemigrations and it shows no migrations to make and I simply do migrate and assume new table is created but then I try to create a superuser and this shows up. So did I miss a step or something? Things that might be informative, I had a previous in built model migrated and trying to create new one by deleting the database file of old one, I have deleted the previous migrations of all other Apps, I have added app and auth_user_model in the settings.

Settings.py

# Custom Model
AUTH_USER_MODEL = "user.CustomUser"

# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Custom
    "home",
    "user",
]

users/models.py

from django.db import models
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, BaseUserManager


class CustomUserManager(BaseUserManager):

    def create_superuser(self, email, password, **other_fields):

        other_fields.setdefault('is_staff', True)
        other_fields.setdefault('is_superuser', True)
        other_fields.setdefault('is_active', True)

        if other_fields.get('is_staff') is not True:
            raise ValueError(
                'Superuser must be assigned to is_staff=True.')
        if other_fields.get('is_superuser') is not True:
            raise ValueError(
                'Superuser must be assigned to is_superuser=True.')

        return self.create_user(email, password, **other_fields)

    def create_user(self, email, password, **other_fields):

        if not email:
            raise ValueError(_('You must provide an email address'))

        email = self.normalize_email(email)
        user = self.model(email=email, **other_fields)
        user.set_password(password)
        user.save()
        return user


class CustomUser(AbstractBaseUser, PermissionsMixin):

    email = models.EmailField(_('email address'), unique=True)
    # user_name = models.CharField(max_length=150, unique=True)
    # first_name = models.CharField(max_length=150, blank=True)
    # start_date = models.DateTimeField(default=timezone.now)
    # about = models.TextField(_(
    #     'about'), max_length=500, blank=True)
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=False)

    objects = CustomUserManager()

    USERNAME_FIELD = 'email'
    # REQUIRED_FIELDS = ['user_name', 'first_name']

    def __str__(self):
        return self.email

Error

Traceback (most recent call last):
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: user_customuser

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

Traceback (most recent call last):
  File "C:\Users\tanis\VScode projects\Cureya\Backend\cureya\manage.py", line 22, in <module>
    main()
  File "C:\Users\tanis\VScode projects\Cureya\Backend\cureya\manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 419, in execute_from_command_line

    utility.execute()
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 79, in ex
ecute
    return super().execute(*args, **options)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\management\base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 113, in h
andle
    error_msg = self._validate_username(username, verbose_field_name, database)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\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 "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\auth\base_user.py", line 45, in get_by_natural_key
    return self.get(**{self.model.USERNAME_FIELD: username})
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 431, in get
    num = len(clone)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 262, in __len__
    self._fetch_all()
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 1324, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\query.py", line 51, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\sql\compiler.py", line 1175, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 98, in execute
    return super().execute(sql, params)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Users\tanis\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\backends\sqlite3\base.py", line 423, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: user_customuser
RevTpark
  • 65
  • 1
  • 8
  • Sometimes, when the `migrations` folder has not been created yet, you have specify the app in `makemigrations`: `python manage.py makemigrations user` for it to detect the model changes. – user2390182 Oct 18 '21 at 18:22
  • Thank you this did the trick, Also if you can provide a in depth explanation that would be great as to I didnt understand why `py manage.py makemigrations` alone didn't do the necessary – RevTpark Oct 18 '21 at 18:29
  • It seems to be [by design](https://docs.djangoproject.com/en/2.0/topics/migrations/#adding-migrations-to-apps) :) I had noticed this trying to make migrations for new apps and nothing got created. Found a dupe, too. – user2390182 Oct 18 '21 at 18:35

0 Answers0