Home > Net >  Update Database Column and Tables with Django
Update Database Column and Tables with Django

Time:07-12

I've encounter into a situation where i try to update mysql database using django. here is the schema of it: the original schema

class Departments(models.Model):
    DepartmentId = models.AutoField(primary_key=True)
    DepartmentName = models.CharField(max_length=100)

class Groups(models.Model):
    GroupId = models.AutoField(primary_key=True)
    GroupName = models.CharField(max_length=100)

class Employees(models.Model):
    EmployeeID = models.AutoField(primary_key=True)
    EmployeeName = models.CharField(max_length=100)
    Departments = models.CharField(max_length=100)
    Groups = models.CharField(max_length=100)
    DateOfRecruitment = models.DateField()
    Position = models.CharField(max_length=100)
    PhotoFileName = models.CharField(max_length=100)

the new schema

class Departments(models.Model):
    DepartmentId = models.AutoField(primary_key=True)
    DepartmentName = models.CharField(max_length=100)

class Groups(models.Model):
    GroupId = models.AutoField(primary_key=True)
    GroupName = models.CharField(max_length=100)

class Positions(models.Model):
    PositionId = models.AutoField(primary_key=True)
    PositionName = models.CharField(max_length=100)

class Employees(models.Model):
    EmployeeID = models.AutoField(primary_key=True)
    EmployeeName = models.CharField(max_length=100)
    Departments = models.CharField(max_length=100)
    Groups = models.CharField(max_length=100)
    DateOfRecruitment = models.DateField()
    DateOfResignation = models.DateField()
    Position = models.CharField(max_length=100)
    Blacklist = models.BooleanField()
    BlacklistDate = models.DateField()
    PhotoFileName = models.CharField(max_length=100)

I've tried to use the following command

python manage.py makemigrations someapp
python manage.py migrate

However it doesn't update within the mysql system itself. The only solution that i came out of is dropping the entire database and make the migration again. I hope that there is a better solution than my method as i couldn't just use the same method for data migration or another table update. Thank you.

CodePudding user response:

You have to use makemigrations everytime you make even a small change in model's fields:

python manage.py makemigrations   # you don't have to use that: someapp

It should create some files in 'migrations' folder in any app, that has at least one difference. Only then second command will make change in database:

python manage.py migrate

CodePudding user response:

Guessing by comments under question you probably haven't added your application to INSTALLED_APPS, thus why makemigrations doesn't detect changes.

  • Related