Askedio / laravel-soft-cascade

Cascade Delete & Restore when using Laravel SoftDeletes
https://medium.com/asked-io/cascading-softdeletes-with-laravel-5-a1a9335a5b4d
MIT License
705 stars 63 forks source link

Invalid query for BelongsTo relationship #109

Closed WimWidgets closed 4 years ago

WimWidgets commented 4 years ago

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();
maguilar92 commented 4 years ago

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.

WimWidgets commented 4 years ago

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

maguilar92 commented 4 years ago

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

WimWidgets commented 4 years ago

I moved on to another similar package since then.

rico commented 4 years ago

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

andrekutianski commented 1 year ago

I moved on to another similar package since then.

can you share the package that you have used?

wimski commented 1 year ago

can you share the package that you have used?

Sorry, but 3.5 years is an eternity in programming. I don't even know which project this was for anymore.