Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Foreign Key Constraints does not work with Doctrine/Symfony and SQLite

I am working with Doctrine 2.4.2, Symfony 2.4 and SQLite 3.8.3.

I have two entities defined:

Category:
    type: entity
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    oneToMany:
        ministries:
            targetEntity: Ministry
            cascade: [persist]
            mappedBy: category

And

Ministry:
    type: entity
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    manyToOne:
        category:
            targetEntity: Category
            inversedBy: ministries
            joinColumn:
                name: category_id
                nullable: false
                onDelete: CASCADE

But when I delete a category, the ministry entities do not get deleted, although the constraint should cascade. What am I missing?

Do I have to configure anything to get that working?

like image 771
naitsirch Avatar asked Mar 20 '23 23:03

naitsirch


1 Answers

You may need to make sure that PRAGMA foreign_keys = ON is set before deleting.

Note that this is a connection setting, not a database setting.

Addition:
With Symfony's event subscriber it is possible to execute this command in the preFlush event of Doctrine, before any writing queries are executed.

like image 179
Mark Baker Avatar answered Mar 22 '23 12:03

Mark Baker