Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
483 views
in Technique[技术] by (71.8m points)

python - Django postgres migration error foreign key constraint does not exist

I'm using a PostgreSQL database hosted with aws, everytime I try to migrate (python manage.py migrate) I run into this error: column "id" referenced in foreign key constraint does not exist.

I have tried removing the two foreign keys that I'm using with no luck I still get the same error. I have also tried this solution with no luck. The rest migrates with no problem, Just this one error

Model.py

class Subprogram(models.Model):
    title = models.CharField(max_length=100)
    program = models.ForeignKey(Program, on_delete=models.CASCADE)
    budget = models.DecimalField(default=0, decimal_places=0, max_digits=11)

class Project(models.Model):
    name = models.CharField(max_length=100)
    budget = models.DecimalField(decimal_places=0, max_digits=11)
    budget_year = models.PositiveIntegerField(default=current_year(), validators=[MinValueValidator(current_year)])
    file = models.FileField(blank=True)
    subprogram = models.ForeignKey(Subprogram, on_delete=models.CASCADE)

class Program(models.Model):
    title = models.CharField(max_length=100)
    date_posted = models.DateTimeField(default=timezone.now)

    def overall_budget(self):
        all_projects = self.subprogram_set.all()
        total = 0
        for project in all_projects:
            total += project.budget
        return total

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('program-detail', kwargs={'pk': self.pk})
question from:https://stackoverflow.com/questions/65865627/django-postgres-migration-error-foreign-key-constraint-does-not-exist

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...