python-3.xdjangodjango-modelsdjango-admindjango-database

OperationalError at /admin/ route of django project


Hey I work with django framework I had the error OperationalError.

I have two classes with the same fields:

class Student(models.Model):
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
    id = models.AutoField(primary_key=True)
    First_Name = models.CharField('First Name', max_length=30, null=True, blank=True)
    Last_Name = models.CharField('Last Name', max_length=30, null=True, blank=True)
    ID_Number = models.CharField('Id Number', max_length=30, null=True, blank=True)
    Phone = PhoneNumberField('Phone',null=True)
    
    class Meta:
        verbose_name_plural = "Students"

    def __str__(self):
        return str(self.user)

class Lecturer(models.Model):
    user = models.OneToOneField(User, null=True, blank=True, on_delete=models.CASCADE)
    id = models.AutoField(primary_key=True)
    First_Name = models.CharField('First Name', max_length=30, null=True, blank=True)
    Last_Name = models.CharField('Last Name', max_length=30, null=True, blank=True)
    ID_Number = models.CharField('Id Number', max_length=30, null=True, blank=True)
    Phone = PhoneNumberField('Phone',null=True)
    
    class Meta:
        verbose_name_plural = "Lecturers"

    def __str__(self):
        return str(self.user)

And I add a new field to my Student and also to Lecturer classes the field is

Phone = PhoneNumberField('Phone',null=True)

And yes I did the commands:

python manage.py makemigrations 
python manage.py migrate

After that I am sure that everything is updated:

Operations to perform:
    Apply all migrations: HomePage, accounts, admin, auth, contenttypes, sessions
Running migrations:
    No migrations to apply.

But when I run the runserver and after that, I go to the admin route:

http://localhost:8000/admin

add I go the Lecturers data everything work great I have new field Phone:

Lecturers

But when I try to go to Student data I get an Error and don't recognize the field's phone!

Student Error


Solution

  • If you don't want to lose db.sqlite3 try to delete migrations first

    Step 1: Delete the db.sqlite3 file.

    Step 2: $ python manage.py migrate

    Step 3: $ python manage.py makemigrations

    Step 4: Create a superuser using $ python manage.py createsuperuser

    new db.sqlite3 will generate automatically.

    Take a look at this entry