添加外键时 Laravel 迁移错误

巫婆

所以我得到这个错误:

Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table yamldb#sql-3928_6ea(errno: 150 "外键约束的格式不正确") (SQL: alter table tblquestionadd constrainttblquestion_que_csd_id_foreign外键 ( que_csd_id) 引用tblcsdomain( csd_id))

表格1

Schema::create('tblquestion', function (Blueprint $table) {
    $table->increments('que_id');
    $table->string('que_name', 128);
    $table->string('que_identifier', 128);
    $table->string('que_version', 50);
    $table->char('que_content');
    $table->char('que_answers');
    $table->integer('que_grd_id')->unsigned();
    $table->integer('que_quf_id')->unsigned();
    $table->integer('que_lan_id')->unsigned();
    $table->boolean('que_mandatory', false);
    $table->char('que_thisisinformatics');
    $table->char('que_translations');
    $table->char('que_explanation');
    $table->char('que_background_info');
    $table->integer('que_cou_id')->unsigned();
    $table->boolean('que_allow_share', false);
    $table->integer('que_source_cou_id')->unsigned();
    $table->integer('que_source_que_id');
    $table->mediumInteger('que_csd_id')->unsigned();
    $table->string('que_token', 32);    
});

表 2

Schema::create('tblcsdomain', function (Blueprint $table) {
    $table->increments('csd_id');
    $table->string('csd_name', 128);
    $table->string('csd_token', 128);
 });

移民

 Schema::table('tblquestion', function (Blueprint $table) {
    $table->foreign('que_csd_id')->references('csd_id')->on('tblcsdomain');
}

此外,我正在尝试将 FK 添加到现有的列中。Laravel 添加了 FK 但在回滚时它不会删除它们。

Schema::table('tblquestion', function (Blueprint $table) {
    $table->dropForeign(['que_csd_id']);
}
詹姆斯·库克

您的外键需要与主键的类型相同。

你要么需要使用

$table->mediumIncrements('csd_id');

在 id 列的表 2 迁移中。或者改变类型

$table->mediumInteger('que_csd_id')->unsigned();

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Laravel 7迁移错误。无法添加外键约束

来自分类Dev

Laravel 5.2委托迁移错误,无法添加外键约束

来自分类Dev

添加迁移错误

来自分类Dev

laravel 5.4 迁移错误号:150“外键约束的格式不正确”

来自分类Dev

Laravel 4迁移错误?

来自分类Dev

Laravel 5.5 迁移错误

来自分类Dev

尝试使用 Laravel 迁移添加外键约束时出错

来自分类Dev

Laravel 5迁移错误1045

来自分类Dev

Laravel使用迁移错误创建表

来自分类Dev

Laravel迁移错误SQLSTATE [42000]

来自分类Dev

Laravel 5.1-迁移错误

来自分类Dev

Laravel工匠的进度迁移错误

来自分类Dev

Laravel 5.3 SQL Server 迁移错误

来自分类Dev

在我的迁移错误中添加参考

来自分类Dev

使用Laravel迁移创建外键时出现MySQL错误

来自分类Dev

使用Laravel迁移创建外键时出现MySQL错误

来自分类Dev

Laravel迁移不会添加外键

来自分类Dev

Laravel迁移无法添加外键

来自分类Dev

laravel迁移添加外键的最佳方法

来自分类Dev

添加Linkedin身份验证时发生迁移错误

来自分类Dev

php artisan在Laravel 4中迁移错误PDOException

来自分类Dev

Laravel工匠在方括号上的迁移错误

来自分类Dev

php Laravel 5.3中的artisan迁移错误

来自分类Dev

迁移Laravel 5.1中的外键错误

来自分类Dev

外键约束格式错误(Laravel迁移)

来自分类Dev

将详细名称添加到外键字段时,django 1.7迁移错误

来自分类Dev

Laravel迁移外键

来自分类Dev

外键 Laravel 迁移

来自分类Dev

EF Core 1.1-preview1添加迁移错误