Askedio/laravel-soft-cascade

Invalid query for BelongsTo relationship

WimWidgets opened this issue · 7 comments

  • Laravel Soft Cascade Version: 6.0.1
  • PHP Version: 7.3.8
  • Database Driver & Version: MySQL 5

Description:

Using soft cascade on a BelongsTo relationship throws a SoftCascadeLogicException. In the example below when soft deleting ModelA, ModelB should be soft deleted and subsequently ModelC. ModelA removing ModelB is not a problem, but cascading ModelC from ModelB generates an invalid query. ModelB in this case acts like a kind of enriched pivot model.

SQLSTATE[42S22]: Column not found: 1054 Unknown column

select count(*) as aggregate
from `models_c`
where `models_b`.`model_c_id` in (?)
  and `models_c`.`deleted_at` is null

Steps To Reproduce:

See code example.

Code example:

class ModelA extends Model
{
    use SoftDeletes;
    use SoftCascadeTrait;
    
    protected $table = 'models_a';
    
    protected $softCascade = [
        'modelB',
    ];
    
    public function modelB(): HasMany
    {
        return $this->hasMany(ModelB::class, 'model_a_id');
    }
}

class ModelB extends Model
{
    use SoftDeletes;
    use SoftCascadeTrait;
    
    protected $table = 'models_b';
    
    protected $softCascade = [
        'modelC',
    ];
    
    public function modelA(): BelongsTo
    {
        return $this->belongsTo(ModelA::class, 'model_a_id');
    }
    
    public function modelC(): BelongsTo
    {
        return $this->belongsTo(ModelC::class, 'model_c_id');
    }
}

class ModelC extends Model
{
    use SoftDeletes;
    use SoftCascadeTrait;
    
    protected $table = 'models_c';
}
$modelA = new ModelA();
$modelA->save();

$modelC = new ModelC();
$modelC->save();

$modelB = new ModelB();
$modelB->modelA->associate($modelA);
$modelB->modelC->associate($modelC);
$modelB->save();

$modelA->delete();

Hi @WimWidgets, first of all thanks for use this package.

Could you say us what version of laravel are you using. I will review it as soon as possible.

I'm using v6.4.1 of the laravel/framework package at the momemt.

@WimWidgets Could you try with the package version 7.0.1. It seems that the problem was there.

I moved on to another similar package since then.

rico commented

@maguilar92 this is still an issue with package version 7.0.1 and Laravel v7.22.4.

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'adressen.mitarbeiter_id' in 'where clause' (SQL: select * from `adressen` where `adressen`.`mitarbeiter_id` = 9 and `adressen`.`mitarbeiter_id` is not null and `adressen`.`deleted_at` is null limit 1)

Querying an adressen.mitarbeiter_id column - which obviously does not exist - when it should query the mitarbeiter.adresse_id column.