Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SoftDelete cause relationship chaining query misplacement #13567

Closed
fzhan opened this issue May 16, 2016 · 15 comments
Closed

SoftDelete cause relationship chaining query misplacement #13567

fzhan opened this issue May 16, 2016 · 15 comments
Labels

Comments

@fzhan
Copy link

fzhan commented May 16, 2016

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

@acasar
Copy link
Contributor

acasar commented May 16, 2016

Which Laravel version? Please include minor version too.

@fzhan
Copy link
Author

fzhan commented May 17, 2016

@acasar version 5.2.31

@acasar
Copy link
Contributor

acasar commented May 17, 2016

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.

@fzhan
Copy link
Author

fzhan commented Jun 6, 2016

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

@themsaid
Copy link
Member

themsaid commented Sep 6, 2016

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

@GrahamCampbell
Copy link
Member

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

@orottier
Copy link

FYI it's still present in v5.3.29

@cansozeri
Copy link

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

@meness
Copy link
Contributor

meness commented May 26, 2017

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)

@GrahamCampbell
Copy link
Member

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

We're open to pull requests.

@bbashy
Copy link
Contributor

bbashy commented Jul 14, 2017

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);
});

@themsaid themsaid added the bug label Oct 26, 2017
@staudenmeir
Copy link
Contributor

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

@staudenmeir
Copy link
Contributor

@laurencei This can be closed.

@mz-devbo
Copy link

mz-devbo commented Aug 8, 2020

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

@staudenmeir
Copy link
Contributor

Laravel 5.8.27 added whereHasMorph(): #28928

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests