我尝试将迁移从整数更改为大整数,反之亦然,但代码仍然给我错误。这是外键的代码:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id('post_id');
$table->biginteger('user_id')->unasigned()->index();
$table->biginteger('company_id')->unasigned()->index();
$table->biginteger('job_id')->unasigned()->index();
$table->timestamps();
});
Schema::table('posts', function($table) {
$table->foreign('user_id')->references('user_id')->on('users');
$table->foreign('company_id')->references('company_id')->on('companies');
$table->foreign('job_id')->references('job_id')->on('jobs');
});
}
用户的代码:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id('user_id');
$table->string('name');
$table->string('email')->unique();
$table->foreignid('companyid');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('status');
$table->string('CV');
$table->rememberToken();
$table->timestamps();
});
}
然后我将其更改为foreignid,没有错误,所以我想知道从表到另一个表调用外键的不同或正确方式是什么?
有几个打字错误。
foreignid
应该是foreignId
- 用
unsigned
代替unasigned
对于post
您可以使用
Schema::create('posts', function (Blueprint $table) {
$table->id('post_id');
$table->foreignId('user_id')->constrained('users');
$table->foreignId('company_id')->constrained('companies');
$table->foreignId('job_id')->constrained('jobs');
$table->timestamps();
});
对于users
,您可以使用
Schema::create('users', function (Blueprint $table) {
$table->id('user_id');
$table->string('name');
$table->string('email')->unique();
$table->foreignId('company_id')->constrained('companies');
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->string('status');
$table->string('CV');
$table->rememberToken();
$table->timestamps();
});
注意:确保
companies
表在users
和posts
表之前创建