# deleteAll causes FK error

**URL:** <https://discourse.cakephp.org/t/deleteall-causes-fk-error/2558>\
**Category:** Need Help\
**Created:** [May 12, 2017, 12:19pm UTC](https://discourse.cakephp.org/t/deleteall-causes-fk-error/2558 "2017-05-12T12:19:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrekat](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@andrekat](https://discourse.cakephp.org/u/andrekat)\
**Post date:** [May 12, 2017, 12:19pm UTC](https://discourse.cakephp.org/t/deleteall-causes-fk-error/2558/1 "2017-05-12T12:19:53Z")

</div>

Following table line-up causes integretiy constraint violation:

```
Error: SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`bradio_one`.`channel_program_spot_blocks`, CONSTRAINT `FK_CHANNEL_PROGRAM_SPOT_ID` FOREIGN KEY (`channel_program_spot_id`) REFERENCES `channel_program_spots` (`channel_program_id`) ON DELETE )

```

Table 1: channel\_programs (parent)  
Table 2: channel\_program\_spots (similar to parent, with additional attributes)  
Table 3: channel\_program\_spot\_blocks (has x entries with different values of attributes)

Table 1 hasOne channel\_program\_spots  
Table 2 hasMany channel\_program\_spot\_blocks

Association setup:

Table 1:

```
 $this->hasOne('ChannelProgramSpots', [
            'foreignKey' => 'channel_program_id',
            'joinType' => 'INNER',
            'dependent' => true,
            'cascadeCallbacks' => true
        ]);

```

Table 2:

```
$this->belongsTo('ChannelPrograms', [
            'foreignKey' => 'channel_program_id',
            'joinType' => 'INNER'
        ]);
    $this->hasMany('ChannelProgramSpotBlocks', [
            'foreignKey' => 'channel_program_spot_id',
            'dependent' => true,
        ]);

```

Table 3:

```
$this->belongsTo('ChannelProgramSpots', [
        'foreignKey' => 'channel_program_spot_id',
        'joinType' => 'INNER',
        'cascadeCallback' => true
    ]);

    $this->hasOne('Spots', [
        'joinType' => 'INNER'
    ]);

```

I have already tried it with different setups: with or without “cascadeCallback”.  
Any suggestions? This thing blows me up …

---

<div class="post-metadata">

**Author:** ![raul338](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/raul338/32/69_2.png) [@raul338](https://discourse.cakephp.org/u/raul338)\
**Post date:** [May 12, 2017, 4:45pm UTC](https://discourse.cakephp.org/t/deleteall-causes-fk-error/2558/2 "2017-05-12T16:45:33Z")

</div>

deleteAll does not do callbacks. You have to loop the entities you want to delete with cascadeCallbacks (in web it might take time)

---

<div class="post-metadata">

**Author:** ![atlet](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/atlet/32/129_2.png) [@atlet](https://discourse.cakephp.org/u/atlet)\
**Post date:** [May 13, 2017, 12:07pm UTC](https://discourse.cakephp.org/t/deleteall-causes-fk-error/2558/3 "2017-05-13T12:07:19Z")

</div>

Why don’t you add “ON DELETE CASCADE” to your foreign key? It’s faster and easier…

---

<div class="post-metadata">

**Author:** ![andrekat](https://avatars.discourse-cdn.com/v4/letter/a/eb9ed0/32.png) [@andrekat](https://discourse.cakephp.org/u/andrekat)\
**Post date:** [May 15, 2017, 10:00am UTC](https://discourse.cakephp.org/t/deleteall-causes-fk-error/2558/4 "2017-05-15T10:00:31Z")

</div>

Wow … never came to this idea. Updated my FK specs and deleting all entries goes so fast now!

Thanks!
