django model migration problem table already exist

36 Views Asked by At

hello i have a legacy database which contain a users table and I want just to add to this table some field and override abstract base user of Django model auth the problem is when I am trying to migrate is says that the table users is already exist in database

from django.contrib.auth.base_user import AbstractBaseUser,BaseUserManager
from django.db import models
import sha512_crypt
import time

# function that return actual timestamp
def actual_epoch_time():
    return int(time.time())
class UserManager(BaseUserManager):
    def create_user(self,email,username,password=None):
        user = self.model(email = normalize_email(email),username=username)
        user.set_password(password)
        user.save()
        return user
    def create_superuser(self,email,username,password=None):
        user = self.create_user(username=username,password=password,email=email)
        user.is_admin = True
        user.save()
        return user

#here is the abstracted user model from xui database with additional fields
class Users(AbstractBaseUser):
    username = models.CharField(max_length=50, blank=True, null=True,unique=True)
    password = models.CharField(max_length=255, blank=True, null=True)
    email = models.CharField(max_length=255, blank=True, null=True)
    ip = models.CharField(max_length=255, blank=True, null=True)
    date_registered = models.IntegerField(default=actual_epoch_time())
    last_login = models.IntegerField(blank=True, null=True)
    member_group_id = models.IntegerField(blank=True, null=True)
    credits = models.FloatField(blank=True, null=True)
    notes = models.TextField(blank=True, null=True)
    status = models.IntegerField(blank=True, null=True)
    reseller_dns = models.TextField(blank=True, null=True)
    owner_id = models.IntegerField(blank=True, null=True)
    override_packages = models.TextField(blank=True, null=True)
    hue = models.CharField(max_length=50, blank=True, null=True)
    theme = models.IntegerField(blank=True, null=True)
    timezone = models.CharField(max_length=255, blank=True, null=True)
    api_key = models.CharField(max_length=64, blank=True, null=True)
 

    objects = UserManager()

    USERNAME_FIELD = 'username'
    class Meta:
        managed = True
        db_table = 'users'

    def set_password(self, raw_password):
        self.password = sha512_crypt.encrypt(raw_password,rounds=200000)
    def check_password(self, raw_password):
        return sha512_crypt.verify(raw_password,self.password)



1

There are 1 best solutions below

1
Mostafa Saleh On

you are trying to extend the user model, in this situation you must drop the database and make it again with new changes for user table, but if you have important data in the database you should first take a backup from it and after dropping the db insert it to the new db, but it always suggested make an abstract model from the user model in the beginning of the project for future changes.