首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >laravel 1005无法创建表`testproject`.`chatter_discussion` (错误号: 150“外键约束形式不正确”)

laravel 1005无法创建表`testproject`.`chatter_discussion` (错误号: 150“外键约束形式不正确”)
EN

Stack Overflow用户
提问于 2021-09-23 20:14:57
回答 2查看 34关注 0票数 0

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

2016_07_29_171128_create_foreign_keys.php

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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 migrate的输出:

代码语言:javascript
复制
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()

我搜索了这个问题,据我所知,我也需要将递增的it设为unsigned,但它不起作用,和以前一样的错误。如果我是正确的,那么外键和创建外键是有问题的,但我不确定具体是什么。提前感谢,请多多关照我是新来拉威尔的

EN

回答 2

Stack Overflow用户

发布于 2021-09-23 20:57:16

我认为错误是由于您的密钥在迁移过程中造成的。

修改引用外键的行,如下所示(在chatter_discussion / chatter_categories文件中):

代码语言:javascript
复制
$table->unsignedBigInteger('YourKeyName')->nullable();
//OR
$table->unsignedBigInteger('YourKeyName'); 
票数 0
EN

Stack Overflow用户

发布于 2021-09-23 23:30:59

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

作者:$table->unsignedBigInteger('chatter_category_id')->default('1');

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69306400

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档