您好我想创建一个外键但没有任何效果,我不明白为什么enter image description here
答案 0 :(得分:0)
请提供有关表类型的更多信息,现在根据您键入的信息我可以建议您检查是否使用InnoDB表?在表1中对应于PK的表1中的FOREIGN KEY?他们有相同的类型和长度吗?
答案 1 :(得分:0)
根据documentation,外键的正确语法是 s 。
Laravel表名通常也是复数,因此检查category
也是正确的。
$table->foreign('category')->references('id')->on('category');
答案 2 :(得分:0)
文章表
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');
}
}
类别表
<?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');
}
}