staudenmeir / eloquent-has-many-deep

Laravel Eloquent HasManyThrough relationships with unlimited levels
MIT License
2.67k stars 157 forks source link

Ambigious column reference 'id' #208

Closed psuet closed 1 year ago

psuet commented 1 year ago

My implementation fails in the following scenario:

We have areas who have buildings who have floors who have rooms All primary keys are called "id" are strings and all foreign_keys are called "whatever_id", where "whatever" is the object referenced, are theirfore also strings.

We now have the class:

class Areas extends Model
{
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public $incrementing = false;
    public $timestamps = false;
    protected $table = 'areas';

    protected $keyType = 'string';

    public function rooms()
    {
        return $this->hasManyDeep(Room::class, [Building::class, Floor::class]);
    }

When accessed this generates the following query:

select * from "areas"."room" inner join "areas"."floor" on "areas"."floor"."id" = "areas"."room"."floor_id" inner join "areas"."building" on "areas"."building"."id" = "areas"."floor"."building_id" where "areas"."building"."area_id" = ? and "id"::text ILIKE ?

Postgres returns in this scenario a Ambiguous column error, since "id" is not well specified. If id is prefixed by "room", the code works as expected.

psuet commented 1 year ago

On further investigation, this is 100% my fault. Sorry for the disruption.