Kynian
Kynian

Reputation: 670

relationship treating foreign key as an integer, not varchar

I've got a table and a view in a postgres database: a customer table and a cust_address_view view. Connecting the two is an id column named customer_id. When I declare the relationship in customer to customerAddress it's treating the customer_id as an integer (or at least it's chopping off the leading zero) causing it to not find the records.

I have the relationship declared as such:

public function customerAddress()
{
    return $this->hasMany('App\CustomerAddress' , 'customer_id', 'customer_id');
}

For customer ID 012345 I'm seeing this query in the debugbar:

select * from "cust_address_view" where "cust_address_view"."customer_id" = '12345' and "cust_address_view"."customer_id" is not null

Is this an issue with one of the two being a view instead of both being a table?

Upvotes: 0

Views: 676

Answers (1)

ashraj98
ashraj98

Reputation: 384

In your Customer class, you want to specify that customer_id cannot increment (i.e. not an integer)

class Customer extends Model
{
    ...
    public $incrementing = false;
    ...
}

That should tell Laravel to not use an integer to lookup the customer.

Upvotes: 3

Related Questions