没有不值得去解决的问题,也没有不值得去学习的技术!

在 Laravel 6 的数据库迁移中,添加外键约束时,Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

在 Laravel 6 的数据库迁移中,添加外键约束时,Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
1、在 Laravel 6 的数据库迁移中,添加外键约束时,Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint。如图1
在 Laravel 6 的数据库迁移中,添加外键约束时,Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
图1


PS E:\wwwroot\object> php artisan module:migrate ThemeStoreDB
Migrating: 2021_12_13_145303_create_theme_asset_version_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `theme_asset_version` add constraint `theme_asset_version_asset_id_foreign` foreign key (`asset_id`) references `theme_asset` (`id`))

  at E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDO\Exception.php:18

  2   Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:119

  Please use the argument -v to see more details.


2、代码实现如下


        Schema::create('theme_asset_version', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('asset_id')->nullable(false)->comment('Theme asset ID');
            $table->foreign('asset_id')->references('id')->on('theme_asset');

        });


3、代码调整如下,仍然报错:


        Schema::create('theme_asset_version', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('asset_id')->nullable(false)->comment('Theme asset ID');
        });

        Schema::table('theme_asset_version', function (Blueprint $table) {
            $table->foreign('asset_id')->references('id')->on('theme_asset');
        });




PS E:\wwwroot\object> php artisan module:migrate ThemeStoreDB
Migrating: 2021_12_13_145303_create_theme_asset_version_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `theme_asset_version` add constraint `theme_asset_version_asset_id_foreign` foreign key (`asset_id`) references `theme_asset` (`id`))

  at E:\wwwroot\object\vendor\laravel\framework\src\Illuminate\Database\Connection.php:669
    665|         // If an exception occurs when attempting to run a query, we'll format the error
    666|         // message to include the bindings with SQL, which will make this exception a
    667|         // lot more helpful to the developer instead of just the database's errors.
    668|         catch (Exception $e) {
  > 669|             throw new QueryException(
    670|                 $query, $this->prepareBindings($bindings), $e
    671|             );
    672|         }
    673|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint")
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDO\Exception.php:18

  2   Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
      E:\wwwroot\object\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:119

  Please use the argument -v to see more details.


4、表 theme_asset_version 的字段 asset_id 类型调整为:unsignedBigInteger,以与 theme_asset 的字段 id 类型保持一致。运行迁移成功。


        Schema::create('theme_asset_version', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('asset_id')->nullable(false)->comment('Theme asset ID');
        });

        Schema::table('theme_asset_version', function (Blueprint $table) {
            $table->foreign('asset_id')->references('id')->on('theme_asset');
        });


 

PHP / Laravel / Yii2 老项目维护与长期技术支持

如果你的 PHP / Laravel / Yii2 项目已经上线,但遇到原开发离职、Bug 长期无人修复、接口不稳定、性能下降、代码难以接手等问题,可以联系我做一次远程技术排查。

适合以下情况:
✅ 老旧 PHP 系统无人维护
✅ Laravel / Yii2 项目 Bug 修复
✅ 后台管理系统小功能迭代
✅ RESTful API 接口排查
✅ MySQL / Redis / Nginx 性能问题
✅ 长期远程兼职维护

可先从一次小问题开始:
✅ 线上报错排查
✅ 接口异常分析
✅ 慢查询与性能瓶颈定位
✅ 代码结构初步评估
✅ 部署环境与日志检查

如需咨询,请联系我,并注明:PHP 维护咨询

联系方式:
Telegram:@shuijingwan
微信:13980074657
邮箱:shuijingwanwq@gmail.com

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理