Php Laravel-删除外键并在一次迁移中添加
我试图在一次迁移中删除外键并添加外键,但无法使其工作: 我已经用Php Laravel-删除外键并在一次迁移中添加,php,mysql,laravel,Php,Mysql,Laravel,我试图在一次迁移中删除外键并添加外键,但无法使其工作: 我已经用外键cms\u id创建了表内容: public function up() { Schema::create('contents', function (Blueprint $table) { $table->increments('id'); $table->integer('ct_id')->unsigned(); $table->foreign(
外键cms\u 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();
});
}
[PDOException]
[PDOException]
以下是库存表的外观:
Schema::create('inventories', function (Blueprint $table) {
$table->increments('id');
$table->integer('remote_id');
$table->integer('local_id')->unsigned();
$table->string('local_type');
$table->timestamps();
});
[PDOException]
[PDOException]
在删除它和创建新外键失败后,我删除了数据库中的所有表,并尝试使用新表再次运行所有迁移,其中包含以下内容:
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');
});
}
[PDOException]
[PDOException]
但是,我得到:
[PDOException]
[PDOException]
[照亮\数据库\查询异常]SQLSTATE[HY000]:一般错误:1215无法添加外键约束 (SQL:alter table
contents
add约束
contents\u-cms\u-id\u-foreign
foreign-k-ey(cms\u-id
)参考
清单
(远程\u id
)
[PDOException]
[PDOException]
[PDO例外]SQLSTATE[HY000]:一般错误:1215无法添加外键约束
[PDOException]
[PDOException]
如果首先运行所有迁移,在其中创建所有表,然后运行新迁移,在其中更改外键,则会出现以下错误:
[PDOException]
[PDOException]
[照亮\数据库\查询异常]SQLSTATE[42000]:语法错误或访问冲突:1091无法删除 “内容系统cms\U id\U外文”;检查列/键是否存在(SQL: 更改表
内容
删除外键
内容\u cms\u id\u外来
)
[PDOException]
[PDOException]
SQLSTATE[42000]:语法错误或访问冲突:1091无法删除
“内容系统cms\U id\U外文”;检查列/键是否存在
[PDOException]
[PDOException]
我甚至尝试只删除数据库中的所有表,只编辑我已经拥有的文件,而不进行新的迁移:
[PDOException]
$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');
[PDOException]
此外,在库存表中:
[PDOException]
$table->integer('remote_id')->unsigned();
[PDOException]
但是,即使那样也不行,我得到:
[PDOException]
[PDOException]
[照亮\数据库\查询异常]SQLSTATE[HY000]:一般错误:1215无法添加外键约束 (SQL:alter table
contents
add约束
contents\u-cms\u-id\u-foreign
foreign-k-ey(cms\u-id
)参考
清单
(远程\u id
)
[PDOException]
[PDOException]
SQLSTATE[HY000]:一般错误:1215无法添加外键约束
[PDOException]
[PDOException]
因此,我检查了所有在互联网上遇到的常见疑点,甚至添加了
$table->engine='InnoDB'
;对于表库存
、内容
和文件
,我确保首先创建表
,然后添加外键
,并检查列是否具有相同的数据类型
,不确定还要做什么?如果外键列的值存在于与该列相关的任何其他表中,因此当您更改外键时,首先您需要删除该值remote\u id
它是inventory
表中的主键吗?我已将inventory表添加到问题中,没有远程id不是主键。您的内容表中有任何数据吗?没有,我已经删除了所有表,并在迁移过程中创建了所有内容,但我不想为其设置单独的迁移文件,以便其他人可以运行php artisan迁移
[PDOException]
[PDOException]