topclaudy / compoships

Multi-columns relationships for Laravel's Eloquent ORM
MIT License
1.12k stars 130 forks source link

belongsto first key is not using foreign key #95

Closed munajaf closed 4 years ago

munajaf commented 4 years ago

hi, it seems like belongsTo are not using foreignkey like the example given currently im using version 2.0.2

Example:

public function a()
    {
        return $this->belongsTo('A', ['foreignKey1', 'foreignKey2'], ['localKey1', 'localKey2']);
    }

because when i tried to do as example in my code

My Code:

    public function order()
    {
        return $this->belongsTo(Order::class, ['id', 'store_id'], ['order_id', 'store_id']);
    }

it returns to me an error saying that Illuminate/Database/QueryException with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'orders.order_id' in 'where clause' (SQL: select * from `orders` where ((`orders`. `order_id` = 1 and `orders`.`store_id` = 2)) and `orders`.`deleted_at` is null)'

which from the error we can clearly see it is not using the foreignkey(id) given on it

Thanks!

topclaudy commented 4 years ago

Try

public function order()
{
    return $this->belongsTo(Order::class, ['order_id', 'store_id'], ['id', 'store_id']);
}

It seems like you got the keys in the wrong order.