首页 > 解决方案 > laravel 1005 无法创建表 `testproject`.`chatter_discussion` (errno: 150 "外键约束格式不正确")

问题描述

您好,我正在使用最新的 Laravel 版本和这个 github 模板https://github.com/thedevdojo/chatter,我正在尝试迁移数据库,但似乎出了点问题,我找不到什么!这是所需的 3 个(我认为)迁移文件和错误:

2016_07_29_171128_create_foreign_keys.php

<?php

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

class CreateForeignKeys extends Migration
{
    public function up()
    {
        Schema::table('chatter_discussion', function (Blueprint $table) {
            $table->foreign('chatter_category_id')->references('id')->on('chatter_categories')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
        });
        Schema::table('chatter_post', function (Blueprint $table) {
            $table->foreign('chatter_discussion_id')->references('id')->on('chatter_discussion')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
            $table->foreign('user_id')->references('id')->on('users')
                        ->onDelete('cascade')
                        ->onUpdate('cascade');
        });
    }

    public function down()
    {
        Schema::table('chatter_discussion', function (Blueprint $table) {
            $table->dropForeign('chatter_discussion_chatter_category_id_foreign');
            $table->dropForeign('chatter_discussion_user_id_foreign');
        });
        Schema::table('chatter_post', function (Blueprint $table) {
            $table->dropForeign('chatter_post_chatter_discussion_id_foreign');
            $table->dropForeign('chatter_post_user_id_foreign');
        });
    }
}

2016_07_29_171118_create_chatter_discussion_table.php

<?php

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

class CreateChatterDiscussionTable extends Migration
{
    public function up()
    {
        Schema::create('chatter_discussion', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('chatter_category_id')->unsigned()->default('1');
            $table->string('title');
            $table->integer('user_id')->unsigned();
            $table->boolean('sticky')->default(false);
            $table->integer('views')->unsigned()->default('0');
            $table->boolean('answered')->default(0);
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('chatter_discussion');
    }
}

2016_07_29_171118_create_chatter_categories_table.php

<?php

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

class CreateChatterCategoriesTable extends Migration
{
    public function up()
    {
        Schema::create('chatter_categories', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('parent_id')->unsigned()->nullable();
            $table->integer('order')->default(1);
            $table->string('name');
            $table->string('color', 20);
            $table->string('slug');
            $table->timestamps();
        });
    }

    public function down()
    {
        Schema::drop('chatter_categories');
    }
}

php artisan 迁移的输出:

Migration table created successfully.
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table (23.72ms)
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table (34.00ms)
Migrating: 2016_07_29_171118_create_chatter_categories_table
Migrated:  2016_07_29_171118_create_chatter_categories_table (18.44ms)
Migrating: 2016_07_29_171118_create_chatter_discussion_table
Migrated:  2016_07_29_171118_create_chatter_discussion_table (27.45ms)
Migrating: 2016_07_29_171118_create_chatter_post_table
Migrated:  2016_07_29_171118_create_chatter_post_table (18.17ms)
Migrating: 2016_07_29_171128_create_foreign_keys

   Illuminate\Database\QueryException

  SQLSTATE[HY000]: General error: 1005 Can't create table `testproject`.`chatter_discussion` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `chatter_discussion` add constraint `chatter_discussion_chatter_category_id_foreign` foreign key (`chatter_category_id`) references `chatter_categories` (`id`) on delete cascade on update cascade)

  at C:\Users\*hidden*\test\testproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
    688▕         // If an exception occurs when attempting to run a query, we'll format the error
    689▕         // message to include the bindings with SQL, which will make this exception a
    690▕         // lot more helpful to the developer instead of just the database's errors.
    691▕         catch (Exception $e) {
  ➜ 692▕             throw new QueryException(
    693▕                 $query, $this->prepareBindings($bindings), $e
    694▕             );
    695▕         }
    696▕     }

  1   C:\Users\*hidden*\test\testproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
      PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `testproject`.`chatter_discussion` (errno: 150 "Foreign key constraint is incorrectly formed")")

  2   C:\Users\*hidden*\test\testproject\vendor\laravel\framework\src\Illuminate\Database\Connection.php:485
      PDOStatement::execute()

我搜索了问题,据我了解,我也需要使递增的 id 无符号,但它没有工作,与以前相同的错误。如果我是正确的,外键和创建外键有问题,但我不确定到底是什么。在此先感谢,请善待我是 Laravel 的新手

标签: phpmysqllaravel

解决方案


尝试替换$table->integer('chatter_category_id')->unsigned()->default('1');
$table->unsignedBigInteger('chatter_category_id')->default('1');

不要忘记回滚或删除数据库上迁移表上的手动表和记录。


推荐阅读