Saturday, 19 January 2019

Why doesn't my model's "on_delete=models.CASCADE," geneate a cascading foreign key constraint?

I'm using Django, Python 3.7, and PostGres 9.5. How do I mark up my model such taht a cascading foreign key constraint gets generated? I currently have this in my models.py file ...

class ArticleStat(models.Model):
    article = models.ForeignKey(Article, on_delete=models.CASCADE, )

When I run "make migrations my_project" in the management console, it produces a file that contains this ...

    migrations.CreateModel(
        name='ArticleStat',
        fields=[
            ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ...
            ('article', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='my_project.Article')),
        ],
    ),

However, when I run the migration (using "migrate my_project 0001"), the resulting foreign key does not contain a cascade delete constraint. This is what the description looks like in PostGres ...

"my_project_articlesta_article_id_a02a5add_fk_my_project" FOREIGN KEY (article_id) REFERENCES my_project_article(id) DEFERRABLE INITIALLY DEFERRED

How else can I get my models.py file to output a cascading delete foreign key constraint?



from Why doesn't my model's "on_delete=models.CASCADE," geneate a cascading foreign key constraint?

No comments:

Post a Comment