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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22692054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:16:07  来源:igfitidea点击:

Laravel: How to set the Primary Key and Foreign Key to string

laravellaravel-4

提问by jrsalunga

I'm not using the auto increment for the id instead i'm using the 32 char unique id. So when i create a relationship and query, im getting a null because my FK expecting int my models

我没有使用 id 的自动增量,而是使用 32 个字符的唯一 id。所以当我创建一个关系和查询时,我得到一个空值,因为我的 FK 期待我的模型

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

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

so when i query this

所以当我查询这个

$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);
});

i get this

我明白了

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)
}

the problem is in the second query i could not retrieve the users info because the user.id expecting int.

问题出在第二个查询中,我无法检索用户信息,因为 user.id 需要 int。

回答by angoru

First, with innoDB you could make those foreing keys without problem

首先,使用 innoDB,您可以毫无问题地制作这些前键

InnoDB allows a foreign key constraint to reference a non-unique key. This is an InnoDB extension to standard SQL.

InnoDB 允许外键约束引用非唯一键。这是标准 SQL 的 InnoDB 扩展。

Mabe you have your tables wrong, try this

也许你的桌子错了,试试这个

For Reservations

预订

    Schema::create('reservations', function($table)
    {
        $table->engine = 'InnoDB';
        $table->string('id', 32)->index();
        $table->string('name', 128);
        $table->string('user_id', 32)->references('id')->on('users');
        $table->timestamps();
    });

for users

对于用户

    Schema::create('users', function($table)
    {
        $table->engine = 'InnoDB';
        $table->string('id', 32)->index();
        $table->string('name', 128);
        $table->timestamps();
    });

then you need to create the relationship in reservations

那么您需要在预订中创建关系

public function user(){
    return $this->belongsTo('User', 'user_id');
}

and now when you search

现在当你搜索

$reservations = Reservation::with('user')->where('user_id', '=', '22beb4892ba944c8b1895855e1d4d1ad')->get();

it must work! i've tested this code.

它必须工作!我已经测试过这段代码。

回答by alvaropgl

In newer versions of Laravel (Im using 6.5.1) you have to set up the key type to string (By default PK type is set to integer)

在较新版本的 Laravel(我使用 6.5.1)中,您必须将键类型设置为字符串(默认情况下,PK 类型设置为整数)

Following the example of the question:

按照问题的例子:

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

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

You can see the example in the laravel documentation

你可以在laravel文档中看到这个例子