laravel 8 (errno: 150 "外键约束格式不正确")

Posted

技术标签:

【中文标题】laravel 8 (errno: 150 "外键约束格式不正确")【英文标题】:laravel 8 (errno: 150 "Foreign key constraint is incorrectly formed") 【发布时间】:2021-12-30 11:43:49 【问题描述】:

我尝试在最后一个 php 和 laravel 8 中创建论坛。我在 laravel 8 中购买了 udemy 课程,我关注他的视频,但在我的电脑中有错误,而视频中没有

2021_11_13_000535_create_posts_table


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    
        Schema::create('posts', function (Blueprint $table) 
            $table->id();
            $table->string('title');
            $table->integer('is_deleted');
            $table->integer('is_approved');
            $table->string('image');
            $table->unsignedBigInteger('discussion_id');
            $table->foreign('discussion_id')->references('id')->on('discussions')->onDelete('cascade');
            $table->unsignedBigInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            $table->string('slug');
            $table->timestamps();
        );
    

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    
        Schema::dropIfExists('posts');
    

2021_11_19_165302_create_discussions_table

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateDiscussionsTable extends Migration

   /**
    * Run the migrations.
    *
    * @return void
    */
   public function up()
   
       Schema::create('discussions', function (Blueprint $table) 
           $table->id();
           $table->string('title');
           $table->string('desc');
           $table->unsignedBigInteger('forum_id');
           $table->foreign('forum_id')->references('id')->on('forums')->onDelete('cascade');
           $table->integer('is_deleted')->default(0);
           $table->string('image')->nullable();
           $table->integer('notify')->default(0);
           $table->unsignedBigInteger('user_id');
           $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

           $table->timestamps();
       );
   

   /**
    * Reverse the migrations.
    *
    * @return void
    */
   public function down()
   
       Schema::dropIfExists('discussions');
   


当我尝试迁移表时出现此错误:

Migrated:  2014_10_12_000000_create_users_table (1,399.45ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (2,117.91ms)
Migrating: 2019_08_19_000000_create_failed_jobs_table
Migrated:  2019_08_19_000000_create_failed_jobs_table (1,592.76ms)
Migrating: 2019_12_14_000001_create_personal_access_tokens_table
Migrated:  2019_12_14_000001_create_personal_access_tokens_table (2,125.96ms)
Migrating: 2021_11_12_234608_create_categories_table
Migrated:  2021_11_12_234608_create_categories_table (2,452.77ms)
Migrating: 2021_11_12_235039_create_forums_table
Migrated:  2021_11_12_235039_create_forums_table (2,849.71ms)
Migrating: 2021_11_13_000340_create_tags_table
Migrated:  2021_11_13_000340_create_tags_table (526.62ms)
Migrating: 2021_11_13_000535_create_posts_table

   Illuminate\Database\QueryException 

  SQLSTATE[HY000]: General error: 1005 Can't create table `stsdb`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `posts` add constraint `posts_discussion_id_foreign` foreign key (`discussion_id`) references `discussions` (`id`) on delete cascade)

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:703
    699▕         // If an exception occurs when attempting to run a query, we'll format the error
    700▕         // message to include the bindings with SQL, which will make this exception a
    701▕         // lot more helpful to the developer instead of just the database's errors.
    702▕         catch (Exception $e) 
  ➜ 703▕             throw new QueryException(
    704▕                 $query, $this->prepareBindings($bindings), $e
    705▕             );
    706▕         
    707▕     

      +9 vendor frames 
  10  database/migrations/2021_11_13_000535_create_posts_table.php:28
      Illuminate\Support\Facades\Facade::__callStatic()

      +21 vendor frames 
  32  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()
 

有人可以解释为什么会出现此错误以及如何解决此问题?因为我不明白我在我的电脑中的视频中有错误,而在视频中没有。有人可以帮助解决这个问题吗?我已经在谷歌中检查过,但我没有找到如何解决这个问题,我按照 tuts 构建论坛 laravel 8 并带有电报通知

【问题讨论】:

表讨论是否已经存在? 是的,都迁移了,只是有错误 你能展示这个迁移吗? discussions.id的数据类型是什么? 我已经编辑了我的主题,我已经显示了所有迁移和添加表格讨论 【参考方案1】:

问题是您的帖子表迁移是在您的讨论迁移之前运行的。

这是因为 Laravel 运行迁移文件名中的时间戳排序:

2021_11_13_000535_create_posts_table -> 13. November
2021_11_19_165302_create_discussions_table -> 19. November

因此,尚未按照我的评论建议创建讨论表!

解决方法很简单,把文件名改成:2021_11_20_000535_create_posts_table -&gt; 20. November

下次请按照我之前的建议查看您的数据库。

来自他们的文档:

生成迁移

您可以使用 make:migration Artisan 命令生成数据库 移民。新的迁移将放置在您的 数据库/迁移目录。每个迁移文件名都包含一个 时间戳,允许 Laravel 确定 迁移:

https://laravel.com/docs/8.x/migrations#generating-migrations

【讨论】:

以上是关于laravel 8 (errno: 150 "外键约束格式不正确")的主要内容,如果未能解决你的问题,请参考以下文章

Laravel 抛出 errno:150 “外键约束的格式不正确”,尽管语法正确

laravel errno 150 外键约束格式不正确

MySQL创建表:错误1005 errno:150“外键约束形成错误”

如何解决mysql代码的errno 150? [复制]

SQLSTATE [HY000] Laravel 8 中的外键约束格式错误

ERROR 1005 (HY000): Can't create table'matrix.system_log' (errno: 150)