gpt4 book ai didi

php artisan 迁移外键错误

转载 作者:行者123 更新时间:2023-11-29 20:43:56 27 4
gpt4 key购买 nike

主表 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]: General error: 1005 Can't create table mydb.#sql-6f4_433 (errno: 150 "Foreign key constraint is incorrectly formed")

注意:我使用的是laravel 5.2,并且mysql类型已经是Innodb,导致错误形成的主要原因是什么

最佳答案

在详细信息表中

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');
});
}

这对你有用

关于php artisan 迁移外键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38503577/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com