deleteAll causes FK error


#1

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 …


#2

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


#3

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


#4

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

Thanks!