SQLSTATE [HY000]:一般错误:1005无法创建表`blog_ db`。#sql-1564_4a`

时间:2019-08-14 18:21:51

标签: php mysql laravel laravel-5 laravel-5.8

我正在尝试创建具有外键约束的迁移文件。是什么导致此错误。

我尝试了很多事情,但问题仍未解决。我已经在Laracasts上尝试过这种方法,但是对我来说不起作用

https://laracasts.com/discuss/channels/laravel/illuminatedatabasequeryexception-sqlstatehy000-general-error-1005-cant-create-table-pgaiiissql-8e8-1ab

public function up()
{
    Schema::create('posts', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('user_id');
        $table->string('post_title', 250);
        $table->string('post_body');
        $table->unsignedBigInteger('category_id');
        $table->string('post_tags')->nullable();
        $table->string('post_slug')->nullable();
        $table->string('post_thumbnail')->nullable();
        $table->tinyInteger('status')->default(1)->comments('0=Disable, 1=enable');
        $table->timestamps();
        $table->foreign('category_id')->references('id')->on('categorys');
        $table->foreign('user_id')->references('id')->on('users');
    });
}

运行迁移命令后,会发生此错误:

Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `blog_
db`.`#sql-1564_4a` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `posts
` add constraint `posts_category_id_foreign` foreign key (`category_id`) references `categorys` (`id`))

  at C:\Users\roven\OneDrive\Desktop\newProject\vendor\laravel\framework\src\Illuminate\Database\Connec
tion.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `blog_db`.`#sql-1564_4a`
(errno: 150 "Foreign key constraint is incorrectly formed")")
      C:\Users\roven\OneDrive\Desktop\newProject\vendor\laravel\framework\src\Illuminate\Database\Conne
ction.php:458

  2   PDOStatement::execute()
      C:\Users\roven\OneDrive\Desktop\newProject\vendor\laravel\framework\src\Illuminate\Database\Conne
ction.php:458

0 个答案:

没有答案