一般错误:1215 无法在 Laravel 中添加外键约束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48856136/
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
General error: 1215 Cannot add foreign key constraint in laravel
提问by m4rii0
Doing the migrations I got this error:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table books
add constraint books_writer_id_foreign
foreign key (writer_id
) references writers
(id
))
I've tried a lot of things but noone looks to work.
做迁移时我得到这个错误:SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table books
add constraint books_writer_id_foreign
foreign key ( writer_id
) writers
( id
)) 我尝试了很多事情,但没有人看起来工作.
2018_02_18_3165165_create_books_table.php
2018_02_18_3165165_create_books_table.php
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateBooksTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('name');
$table->text('description');
$table->integer('numPages');
$table->enum('language', ['spanish', 'english']);
$table->date('wrote_date')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
});
Schema::table('books', function (Blueprint $table) {
$table->integer('writer_id')->unsigned();
$table->foreign('writer_id')->references('id')->on('writers');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('books');
}
}
2018_02_18_192915_create_writers_table
2018_02_18_192915_create_writers_table
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateWritersTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('writers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->string('nationality');
$table->date('year_date')->nullable();
$table->date('dead_date')->nullable();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('writers');
}
}
Edit: The error was because the first migration was the books, and then the writers causing that the error.
编辑:错误是因为第一次迁移是书籍,然后是导致错误的作者。
回答by AH.Pooladvand
Sometimes based on my experience too close timestamps will break the code and will throw an exception because program thinks that writers table is created after books table try to change writers_table timestamp something like:
2018_02_16_31615
有时根据我的经验,太接近的时间戳会破坏代码并抛出异常,因为程序认为在书籍表尝试更改 writers_table 时间戳之后创建了 writers 表,例如:
2018_02_16_31615
回答by Mahdi Younesi
Make it unsignedInteger
because you're using increments()
这样做是unsignedInteger
因为你正在使用increments()
$table->unsignedInteger('writer_id')->nullable();
回答by Alexey Mezenin
Move this line to the first closure:
将此行移至第一个闭包:
$table->integer('writer_id')->unsigned();
Put it right after:
放在后面:
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
Also, you need to add timestamps to both migration files so Laravel first create the writers
table and then the books
table:
此外,您需要为两个迁移文件添加时间戳,以便 Laravel 首先创建writers
表,然后创建books
表:
2018_02_01_000000_create_writers_table.php
2018_02_02_000000_create_books_table.php