gpt4 book ai didi

php - 无法添加外键

转载 作者:行者123 更新时间:2023-11-29 10:00:29 25 4
gpt4 key购买 nike

我正在尝试将外键添加到laravel中的表中,并且出现此错误

In Connection.php line 664:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `bugFix` add constraint `bugfix_project_id_foreign` foreign k
ey (`project_id`) references `Project` (`id`))


In Connection.php line 458:

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint


我搜索了类似于ine的问题,每个人都要求将数据库引擎设置为InnoDB,但是我确实做到了,但是错误仍然存​​在。这是我用于创建bugFix表的迁移脚本

public function up()
{
Schema::create('bugFix', function (Blueprint $table) {
$table->engine = 'InnoDB';
...
$table->unsignedInteger('project_id');
...
$table->foreign('project_id')
->references('id')->on('Project');
...
});
}


这是创建项目表的脚本

public function up()
{
Schema::create('project', function (Blueprint $table) {
$table->engine = 'InnoDB';
// Project document information
$table->increments('id');
...
});
}

最佳答案

尝试更改您的unsigned integer declaration迁移
并且表名应为准确的拼写Project => project

public function up()
{
Schema::create('bugFix', function (Blueprint $table) {
$table->engine = 'InnoDB';
...
$table->integer('project_id')->unsigned();
...
$table->foreign('project_id')
->references('id')->on('project');
...
});
}

关于php - 无法添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53094512/

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