Laravel:如何将主键和外键设置为字符串

问题描述 投票:1回答:2

我没有使用ID的自动递增,而是使用了32个字符的唯一ID。因此,当我创建一个关系并进行查询时,由于我的FK期望int,我得到的是null我的模特

class User extend Eloquent {
    public $incrementing = false; 
}

class Reservation extend Eloquent {
    public $incrementing = false; 
}

所以当我查询这个

$reservations = Reservation::with('user')->where('user_id', '=', '22beb4892ba944c8b1895855e1d4d1ad')->get();
i could not retrieve the users information but the reservations info is working fine
when i try to listen for query. eg:
Event::listen('illuminate.query', function($query, $bindings, $time, $name){
    var_dump($query);
    var_dump($bindings);
});

我明白了

string(46) "select * from `reservation` where `user_id` = ?"
array(1) {
  [0]=>
  string(36) "22beb4892ba944c8b1895855e1d4d1ad"
}
string(53) "select * from `user` where `user`.`id` in (?)"
array(1) {
  [0]=>
  int(0)
}

问题出在第二个查询中,因为user.id期望为整数,我无法检索用户信息。

laravel laravel-4
2个回答
6
投票
InnoDB允许外键约束引用非唯一键。这是对标准SQL的InnoDB扩展。

0
投票
© www.soinside.com 2019 - 2024. All rights reserved.