Search code examples
phpsqlitesymfonydoctrine-orm

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?


Solution

  • 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.