Hi i want to create a foreign key but nothing work and i don't understand whyenter image description here
嗨,我想创建一个外键但没有任何工作,我不明白为什么在这里的图像描述
在此处输入图像描述
3 个解决方案
#1
0
Please provide more info about table type, for now based on info that you type I can suggest you check are you using InnoDB tables? does FOREIGN KEY in table one corresponding to PK in table 2? are they have same types and length?
请提供有关表类型的更多信息,现在根据您键入的信息,我建议您检查是否使用InnoDB表?在表1中对应于表2中的PK的FOREIGN KEY?他们有相同的类型和长度?
#2
0
The correct syntax for a foreign key is references, per the documentation.
根据文档,外键的正确语法是引用。
Also Laravel table names are typically pluralised, so check category
is correct too.
Laravel表名称通常也是复数,因此检查类别也是正确的。
$table->foreign('category')->references('id')->on('category');
#3
0
articles table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title' , 255);
$table->text('content');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
category table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('category', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('category');
}
}
#1
0
Please provide more info about table type, for now based on info that you type I can suggest you check are you using InnoDB tables? does FOREIGN KEY in table one corresponding to PK in table 2? are they have same types and length?
请提供有关表类型的更多信息,现在根据您键入的信息,我建议您检查是否使用InnoDB表?在表1中对应于表2中的PK的FOREIGN KEY?他们有相同的类型和长度?
#2
0
The correct syntax for a foreign key is references, per the documentation.
根据文档,外键的正确语法是引用。
Also Laravel table names are typically pluralised, so check category
is correct too.
Laravel表名称通常也是复数,因此检查类别也是正确的。
$table->foreign('category')->references('id')->on('category');
#3
0
articles table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticlesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title' , 255);
$table->text('content');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
category table
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateCategoryTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('category', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('category');
}
}