我在为表迁移时设置外键时遇到问题。 这是代码:
public function up()
{
Schema::table('ticket', function (Blueprint $table) {
$table->integer('respondent_id')->nullable();
$table
->foreign('respondent_id', 'ticket_respondent_id_foreign')
->references('users')->on('id')->onDelete('set null');
});
}
public function down()
{
Schema::table('ticket', function (Blueprint $table) {
$table->dropForeign('ticket_respondent_id_foreign');
$table->dropColumn('respondent_id');
});
}
错误:
Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规错误: 1005无法创建表
在lamourka
。#sql-1_628
(错误号:150“ Foreign 键约束的格式不正确”)(SQL:alter tableticket
add 约束ticket_respondent_id_foreign
外键 (respondent_id
)在删除集上引用了id
(users
)/var/www/html/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664
异常跟踪:
1 Doctrine \ DBAL \ Driver \ PDOException::(“” SQLSTATE [HY000]:常规 错误:1005无法创建表
lamourka
。#sql-1_628
(错误号:150 “外键约束的格式不正确”)“) /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:1192 PDOException::(“” SQLSTATE [HY000]:常规错误:1005无法 创建表
lamourka
。#sql-1_628
(错误号:150“外键 约束的格式不正确“)”) /var/www/html/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOStatement.php:117