主表POll
public function up()
{
Schema::create('poll', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->string('poll_question', 255);
$table->boolean('status',1)->default(0)->index();
$table->unsignedInteger('order');
});
}
明细表
public function up()
{
Schema::create('poll_option', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('poll_id');
$table->string('poll_option', 255);
$table->unsignedInteger('vote_poll_option');
$table->unsignedInteger('order');
$table->boolean('status',1)->default(0)->index();
$table->foreign('poll_id')->references('id')->on('poll')->onUpdate('cascade')->onDelete('cascade');
});
}
当我用外键运行php artisan时
SQLSTATE [HY000]:常规错误:1005无法创建表
mydb
。#sql-6f4_433
(错误号:150“外键约束形成错误”)
注意:我使用 laravel 5.2 和mysql类型已经Innodb错误形成的主要原因是什么
答案 0 :(得分:0)
public function up()
{
Schema::create('poll_option', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('poll_id')->unsigned(); //Change Here
$table->string('poll_option', 255);
$table->unsignedInteger('vote_poll_option');
$table->unsignedInteger('order');
$table->boolean('status',1)->default(0)->index();
$table->foreign('poll_id')
->references('id')->on('poll')
->onDelete('CASCADE')
->onUpdate('CASCADE');
});
}
这对你有用