gpt4 book ai didi

Laravel 4 迁移 - 无法添加外键约束

转载 作者:行者123 更新时间:2023-12-04 05:39:20 25 4
gpt4 key购买 nike

这是我的迁移代码:

public function up()
{
Schema::create('foos', function(Blueprint $table) {
// Primary key
$table->increments('id');

// Standard
$table->engine = 'InnoDB';
$table->timestamps();
$table->softDeletes();
});

Schema::create('bars', function(Blueprint $table) {
// Primary key
$table->increments('id');

// Define foreign key
$table->integer('foo_id')->unsigned;

// Foreign key contraints
// NOTE: causes "General error: 1215 Cannot add foreign key constraint"
// $table->foreign('foo_id')->references('id')->on('foos');

// Standard
$table->engine = 'InnoDB';
$table->timestamps();
$table->softDeletes();
});
}

public function down()
{
Schema::drop('foos');
Schema::drop('bars');
}

当定义外键约束的代码没有注释掉时,我在命令行上收到以下错误: 一般错误:1215 无法添加外键约束 .

任何想法我做错了什么?

最佳答案

$table->integer('foo_id')->unsigned;

应该
$table->integer('foo_id')->unsigned();

或者您可以使用简短版本:
$table->unsignedInteger('foo_id');

关于Laravel 4 迁移 - 无法添加外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18995337/

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