Framework: SoftDelete cause relationship chaining query misplacement

Created on 16 May 2016  路  15Comments  路  Source: laravel/framework

Class Address extends SoftDeletes() {
public function addressable()
{
return $this->morphTo();
}
public function scopeSpace($query) {
return $query->where('addressable_type' ,'spaces');
}
}

Address::space()->has('addressable');
Result in "
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'self_16c3ee36946631a2bc5bd5c263214040.deleted_at' in 'where clause' (SQL: select * from address where addressable_type = spaces and exists (select * from address as self_16c3ee36946631a2bc5bd5c263214040 where self_16c3ee36946631a2bc5bd5c263214040.id = self_16c3ee36946631a2bc5bd5c263214040.addressable_id and self_16c3ee36946631a2bc5bd5c263214040.deleted_at is null) and self_16c3ee36946631a2bc5bd5c263214040.deleted_at is null)
"

Where the last self_16c3ee36946631a2bc5bd5c263214040.deleted_at is null should have been address.deleted_at is null

bug

All 15 comments

Which Laravel version? Please include minor version too.

@acasar version 5.2.31

Can you first update to the latest and then paste the query it gives you? Because the has query was changed a little in 5.2.35.

@acasar
I've updated to 5.2.35 here's the new result:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'laravel_reserved_0.deleted_at' in 'where clause' (SQL: select * fromaddresswhereaddressable_type= spaces and exists (select * fromaddressaslaravel_reserved_0wherelaravel_reserved_0.id=laravel_reserved_0.addressable_idandlaravel_reserved_0.deleted_atis null) andlaravel_reserved_0.deleted_atis null)

It's a matter of softdelete, which the last bit should not be 'laravel_reserved_0', rather should be 'address'.

@fzhan does this issue still exist in the latest version of 5.2?

Or indeed 5.3. 5.2 is technically closed for fixes now.

FYI it's still present in v5.3.29

it's still present in 5.4. Any solution ??

I'm using v5.4.23 and the issue still exists.

The Model

class Request extends Model {

    /**
     * @var array
     */
    protected $dates = [
        'created_at',
        'deleted_at',
    ];

    use SoftDeletes;

    /**
     * @var array
     */
    protected $guarded = [
        'id',
        'created_at',
        'deleted_at',
    ];

    /**
     * @Relations
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
     */
    public function owner(): MorphTo {

        return $this->morphTo();
    }
}

The Scope

 * @param \Illuminate\Database\Eloquent\Builder $builder
 * @param \Illuminate\Support\Collection        $skills
 *
 * @return \Illuminate\Database\Eloquent\Builder
 */
public function scopeRelatedCollaborations(Builder $builder, Collection $skills) {
    return $builder->whereHas('owner', function (Builder $builder) use($skills)
    {
        // empty
    });
}

The Query Exception

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'laravel_reserved_0.deleted_at' in 'where clause' (SQL: select * from requests where exists (select * from requests as laravel_reserved_0 where laravel_reserved_0.id = laravel_reserved_0.owner_id and laravel_reserved_0.deleted_at is null) and laravel_reserved_0.deleted_at is null)

I'm using v5.4.23 and the issue still exists.

We're open to pull requests.

Just came across this too. Don't think I know it well enough to do a PR though.

Workaround was to use the local key instead of going into the polymorphic relationship.

$query->whereHas('periodical', function ($query) use ($type_id) {
    $query->where('typeable_id', $type_id);
});

Instead of

$query->whereHas('periodical.typeable', function ($query) use ($type_id) {
    $query->where('id', $type_id);
});

Using SoftDeletes is not the problem. Queries like this can't work because Laravel doesn't support whereHas() on MorphTo relationships (#5429, #18523).

@laurencei This can be closed.

Just in case somebody stumbles with this issue, I've found a nice workaround here:

https://medium.com/better-through-code/using-wherehas-in-laravel-polymorphic-relations-2894f43217e4

Laravel 5.8.27 added whereHasMorph(): #28928

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Anahkiasen picture Anahkiasen  路  3Comments

JamborJan picture JamborJan  路  3Comments

felixsanz picture felixsanz  路  3Comments

SachinAgarwal1337 picture SachinAgarwal1337  路  3Comments

kerbylav picture kerbylav  路  3Comments