首页 > 解决方案 > 一般错误:1005 无法创建表`learning`.`users`

问题描述

迁移我的数据库时出现此错误,下面是我的代码,后面是我在尝试运行迁移时遇到的错误

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();

        $table->string('first_name')->nullable();
        $table->string('last_name')->nullable();
        $table->string('city')->nullable();
        $table->unsignedBigInteger('role_id');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');

    });
}

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

标签: laravel-6.2

解决方案


错误 1005 与外键有关。确保您已经有角色表,否则它将在php artisan migrate上创建用户表 之前运行,只需通过编辑文件名将角色迁移文件放在用户之前。

2019_12_07_000000_create_roles_table.php
2019_12_08_000000_create_users_table.php

推荐阅读