Search code examples
pythondjangodjango-custom-useroperationalerror

django.db.utils.OperationalError: no such table: MainApp_user when i try to create superuser


I make some web application, and almost in the end of development process i decided to customize user model to make profile page with many other info. Recently i have found a video where example from django docs is explained, and i made the same code as there was, but at first i delete my file db.sqlite3 and now when i try to create a superuser i do always catch the next error: django.db.utils.OperationalError: no such table: MainApp_user

Here is my models.py:

class MyUserManager(BaseUserManager):
    def create_user(self, username, password):
        if not username:
            raise ValueError("Mailname field is empty")
        if not password:
            raise ValueError("You have to set password")

        user = self.model(
            username=username, 
            )
        user.set_password(password)
        user.save(using=self._db)
        return user


class User(AbstractBaseUser):
    username            = models.CharField(max_length=30, unique=True)
    password            = models.CharField(max_length=64)

    name                = models.CharField(max_length=30, blank=True)
    surname             = models.CharField(max_length=30, blank=True)  
    
    avatar              = models.ImageField(width_field=512, height_field=512)

    email_link          = models.CharField(max_length=64, blank=True, null=True)
    bio                 = models.CharField(max_length=512, blank=True, null=True)
    
    registered          = models.DateField(auto_now_add=True)

    USERNAME_FIELD      = "username"
    REQUIRED_FIELDS     = ['password']
    objects             = MyUserManager()

    def __str__(self):
        return self.username

Also i've added the next variable in settings.py:

AUTH_USER_MODEL = "MainApp.User"

Why this error happens and how to solve it, help please.

***Ofc, i did made migrations to database


Solution

  • I did found the solution. In my case, when i have deleted db.sqlite3 file i've also deleted "migrations" folder in the app. Of course, i've created it again, but the issue was just in that i forgot to add init.py in migrations folder. And django after migrations applying made only system tables in database(auth_users, contenttypes and other), it didn't save my own models. So that if you ever come up with any similar problem, remember about init.py in migrations folder. Good luck !