Skip to content

Ambiguous column for "whereHas conditions" in hybrid relations with a "join" #1876

Open
@anderson-comvex

Description

@anderson-comvex

Description

The issue occurs with the following query, when you join the two relational database models and attempt to use the whereHas into the third relation which is a MongoDB Model.

Example

class Tree extends Illuminate\Database\Eloquent\Model
{
    use HybridRelations;

    public function apples(): HasMany
    {
        return $this->hasMany(Apple::class, 'tree_id');
    }

    public function seeds(): HasMany
    {
        return $this->hasMany(Seed::class, 'tree_id');
    }
}
class Apple extends Illuminate\Database\Eloquent\Model
{
    use HybridRelations;

    public function tree(): BelongsTo
    {
        return $this->belongsTo(Tree::class, 'tree_id');
    }

    public function seeds(): HasMany
    {
        return $this->hasMany(Seed::class, 'apple_id');
    }
}
class Seed extends Jenssegers\Mongodb\Eloquent\Model
{
    public function tree(): BelongsTo
    {
        return $this->belongsTo(Tree::class, 'tree_id');
    }

    public function apple(): BelongsTo
    {
        return $this->belongsTo(Apple::class, 'apple_id');
    }
}
Tree::query()->first()->apples()->whereHas('seeds',
    function ($query)
    {
       $query->whereIn('type', ['grannysmith']);
    }
);

Exception

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs investigationNeed investigation about bugs described in issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions