django.db.utils.OperationalError: (1091, "Can't DROP 'company_id'; check that column/key exists")

Viewed 2890

When I migrate, i always get it error. First, I created the table and then dropped it. Now when I created the table again. I am facing this issue. It also does not create the columns.

Model.py

class AssociateCompany(models.Model):
    user = models.ForeignKey(User, related_name="associate_user", on_delete=models.CASCADE)
    company = models.ForeignKey(Company, related_name="associate_company", on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

It does not Create the column, user_id and company_id, and displays the folling error:

django.db.utils.OperationalError: (1091, "Can't DROP 'company_id'; check that column/key exists")

2 Answers

You can take a look at the py file in the migrations folder and comment out the erroneous remove in the operations list operate.

The easiest way to solve this is to remake the database from scratch.

  1. If you are using SQLite. Delete the file and delete all files from the migration folder and try creating the migration file again.
  2. If you are using MySQL or PostgreSQL delete all the tables and try again. don't forget to delete the migration files else this won't work. This problem arises when you had a foreign key and you deleted the column without removing the depending data.
Related