我正在尝试删除外键并在一次迁移中添加外键但无法使其工作:
我使用contents
:
foreign key cms_id
public function up()
{
Schema::create('contents', function (Blueprint $table) {
$table->increments('id');
$table->integer('ct_id')->unsigned();
$table->foreign('ct_id')->references('id')->on('content_types');
$table->integer('cms_id')->unsigned();
$table->foreign('cms_id')->references('id')->on('inventories');
$table->string('title');
$table->string('slug');
$table->text('excerpt');
$table->mediumText('body');
$table->string('password');
$table->integer('parent_id');
$table->timestamps();
});
}
这就是库存表的样子:
Schema::create('inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('remote_id');
$table->integer('local_id')->unsigned();
$table->string('local_type');
$table->timestamps();
});
在没有删除它并创建新的外键之后,我删除了数据库中的所有表,并尝试使用新的表再次运行所有迁移,这包含:
public function up()
{
Schema::table('inventories', function (Blueprint $table) {
$table->integer('remote_id')->unsigned()->change();
});
Schema::table('contents', function (Blueprint $table) {
$table->dropForeign('contents_cms_id_foreign');
$table->foreign('cms_id')->references('remote_id')->on('inventories');
});
Schema::table('files', function (Blueprint $table) {
$table->dropForeign('files_cms_id_foreign');
$table->foreign('cms_id')->references('remote_id')->on('inventories');
});
}
但是,我明白了:
[照亮\数据库\ QueryException]
SQLSTATE [HY000]:常规错误:1215无法添加外键约束 (SQL:alter tablecontents
添加约束contents_cms_id_foreign
外国人(cms_id
)引用inventories
(remote_id
))[PDOException]
SQLSTATE [HY000]:常规错误:1215无法添加外键约束
如果,我首先运行我创建所有表的所有迁移,然后运行新的迁移,我更改了外键,我收到此错误:
[照亮\数据库\ QueryException]
SQLSTATE [42000]:语法错误或访问冲突:1091无法删除 '内容s_cms_id_foreign&#39 ;;检查列/键是否存在(SQL: alter tablecontents
删除外键contents_cms_id_foreign
)[PDOException]
SQLSTATE [42000]:语法错误或访问冲突:1091无法删除 '内容s_cms_id_foreign&#39 ;;检查列/密钥是否存在
我甚至试图删除数据库中的所有表,只编辑我已经拥有的文件,只需更改,而不进行新的迁移:
$table->integer('ct_id')->unsigned();
$table->foreign('ct_id')->references('remote_id')->on('content_types');
$table->integer('cms_id')->unsigned();
$table->foreign('cms_id')->references('remote_id')->on('inventories');
而且,在库存表中:
$table->integer('remote_id')->unsigned();
但是,即便如此,它也不会起作用,我得到了:
[照亮\数据库\ QueryException]
SQLSTATE [HY000]:常规错误:1215无法添加外键约束 (SQL:alter tablecontents
添加约束contents_cms_id_foreign
外国人(cms_id
)引用inventories
(remote_id
))[PDOException]
SQLSTATE [HY000]:常规错误:1215无法添加外键约束
所以,我检查了我在互联网上遇到的所有常见嫌疑人,我甚至添加了$table->engine = 'InnoDB'
;对于表inventories
,contents
和files
,我确保首先创建tables
,然后添加foreign keys
,并检查列是否有相同的data type
,不知道该怎么办?