0 votes
in Programming by (400 points)

I am working with a legacy database and I have created a custom user model. I am working to set up the register and authentication funcs. I have created the user manager and in user model there are some fields that I have added for django like is_staff, is_active, date_joined. When I run migrations, the legacy table still does not have the columns I have added in the model. Should It actually alter the legacy database?

class TbUser(AbstractBaseUser, PermissionsMixin):
    id = models.CharField(primary_key=True, max_length=40)
    usname = models.CharField(max_length=40, blank=True, null=True, unique=True)
    psword = models.CharField(max_length=255, blank=True, null=True)
  
    # added columns
    is_staff = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    date_joined = models.DateTimeField(default=timezone.now)

    objects = TbUserManager()

    USERNAME_FIELD = 'usname'
    REQUIRED_FIELDS = []

    class Meta:
        managed = False
        db_table = 'tb_user'

Also, when I am creating the superuser, I get the following error

django.db.utils.OperationalError: (1054, "Unknown column 'tb_user.password' in 'field list'")

Although The user manager looks like this

class TbUserManager(BaseUserManager):
    
    def create_user(self, email, psword=None, **kwargs):
        if not email:
            raise ValueError('Users must have a valid email address.')
        if not kwargs.get('usname'):
            raise ValueError('Users must have a valid username.')
        user = self.model(
            email=self.normalize_email(email), usname=kwargs.get('usname')
        )
        user.psword(psword)
        user.save()
        return user

    def create_superuser(self, email, psword, **kwargs):
        user = self.create_user(email, psword, **kwargs)

        user.is_superuser = True
        user.save()

        return user

I really dont know where the error found the tb_user.password because I have renamed all to psword

If you need some details feel free to ask.

Please log in or register to answer this question.

...