4

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 Answers2

1

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

Liukersun
  • 21
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 05:57
-2

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.
hrtj123
  • 11
  • 2
  • 2
    Hi @hrtj123, I don't see how this is a solution. It's like saying if you want to fix a car, just make it disappear and get a new one. :-) – cvicente Jul 13 '22 at 15:28
  • It is a sort of solution for beginners. NOT for production. – Blind2k Apr 19 '23 at 13:57