Laravel 迁移错误 外键约束格式不正确

发布于 2025-01-19 09:51:11 字数 2040 浏览 4 评论 0原文

这是每次显示的错误。

   Illuminate\Database\QueryException 

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

  1   G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
      PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `codehacking`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed"))
  2   G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
      PDOStatement::execute()

用户表

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->integer('role_id')->index()->unsigned()->nullable();
            $table->integer('is_active')->default(0);
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

帖子表

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->unsigned()->index();
            $table->integer('category_id')->unsigned()->index();
            $table->integer('photo_id')->unsigned()->index();
            $table->string('title');
            $table->string('body');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

我每次尝试迁移表时都会出现此错误。如何解决它? 该代码基于Laravel-9。每当我遇到此错误时,我都在尝试使用PHP Aritsan迁移命令创建表。

This is the error showing every time.

   Illuminate\Database\QueryException 

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

  1   G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
      PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `codehacking`.`posts` (errno: 150 "Foreign key constraint is incorrectly formed"))
  2   G:\Laravel\CodeHacking\vendor\laravel\framework\src\Illuminate\Database\Connection.php:501
      PDOStatement::execute()

users table

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->integer('role_id')->index()->unsigned()->nullable();
            $table->integer('is_active')->default(0);
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

posts table

    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->integer('user_id')->unsigned()->index();
            $table->integer('category_id')->unsigned()->index();
            $table->integer('photo_id')->unsigned()->index();
            $table->string('title');
            $table->string('body');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }

I am having this error every time I try to migrate my table. How to solve it.?
This code is based on laravel-9. I am trying to create tables with the php aritsan migrate command every time I am getting this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叫嚣ゝ 2025-01-26 09:51:11
Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->integer('category_id')->unsigned()->index();
            $table->integer('photo_id')->unsigned()->index();
            $table->string('title');
            $table->string('body');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->nullable();
            $table->integer('category_id')->unsigned()->index();
            $table->integer('photo_id')->unsigned()->index();
            $table->string('title');
            $table->string('body');
            $table->timestamps();

            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
水中月 2025-01-26 09:51:11

我也有这个错误。当外键类型及其参考类型与不匹配时,此错误将处理。例如,如果外键类型为(int),但是参考密钥类型为(biginteger)此错误将处理。

I had this error too . This error will handle when foreign key type and its reference type are not match . For example , if foreign key type is (int) but reference key type is (biginteger) this error will handle.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文