Forums

Cannot drop column needed in a Foreign key Constraint

Hi All,

I had the DB working and I made some changes to my models.py file in order to link three different tables together via foreign key, this didn't really work as I expected, so I reverted my models.py file and tried to re-apply the migrations. Which presented me with the above error.

However now I just cant get past it. I have dropped the DB in the MYSQL shell and recreated, i've even re-created under a different name, out of desperation I even dropped the default db that python anywhere creates.

When running python manage.py migrate, it gets so far before erroring out. Any ideas / help?

OperationalError: (1829, "Cannot drop column 'id': needed in a foreign key constraint 'sp_scraper_dbsession_eventid_id_a8e54bf4_fk_sp_scrape' of table ......

Those don't exist in models.py anymore and I have deleted the db so a little confused as to why there is still an error:

models.py

from django.db import models

class dbevent(models.Model):
    url = models.CharField(max_length=250)
    title = models.CharField(max_length=250)
    date = models.CharField(max_length=50)

    class Meta:
        unique_together = ("url", "title")

class dbsessions(models.Model):
    url = models.CharField(max_length=250)
    date = models.CharField(max_length=50)
    name = models.CharField(max_length=250)
    time = models.CharField(max_length=50)
    sessclass = models.CharField(max_length=250)

class dbresults(models.Model):
    bestlap = models.CharField(max_length=5)
    besttime = models.CharField(max_length=20)
    competitorname = models.CharField(max_length=250)
    diff = models.CharField(max_length=20)
    laps = models.CharField(max_length=20)
    position = models.CharField(max_length=20)
    racenumber = models.PositiveSmallIntegerField
    topspeed = models.CharField(max_length=20)
    totaltime = models.CharField(max_length=20)
    winner = models.BooleanField

[edit by admin: formatting]

If you've completely deleted the database, and you don't have any other databases using the same code that you rely on (eg. one on your local machine) then perhaps the easiest way out of the problem is to delete all of the existing migrations (that is, the files inside the migrations directories of each of your apps), then run manage.py makemigrations then migrate again.

That was it!, brilliant! - thankyou

Great! Glad to help :-)