繁体   English   中英

PHP artisan migration命令给出错误?

[英]PHP artisan migrate command giving errors?

当我运行php artisan migrate命令时,会导致以下错误

[Illuminate \\ Database \\ QueryException] SQLSTATE [42S01]:基本表或视图已存在:1050表'users'已存在(SQL:创建表usersid int unsigned不为null auto_increment主键, name varchar(255)不为null, email VARCHAR(255)NOT NULL, password为varchar(255)NOT NULL, remember_token为varchar(100)空, created_at时间戳空, updated_at时间戳空)默认字符集utf8mb4整理utf8mb4_unicode_ci)

[PDOException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists

我在Windows上使用Laravel 5.4。

让您知道“用户”表已经存在-当您运行迁移时-它正在尝试创建该表(但它已经存在)

这通常是因为您之前曾尝试运行命令'php artisan migration'。 必须回滚此操作以“撤消”这些更改,或清除数据库表。

您可以运行:

php artisan migrate:rollback 

那应该摆脱所有表-然后您可以运行

php artisan migrate

并且应该适当地加载所有内容。

替代方法? 登录到数据库,然后从数据库中物理删除这些表。 然后,您可以再次运行迁移,它将正常运行。

一点陷阱:不过请先检查您的迁移:它应该具有以下功能:

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

如果没有,回滚将不会删除用户表,并且仍然会出现此问题(这意味着您必须登录数据库并手动删除表)。

如果使用此命令创建迁移,则回退功能将自动包括在内:

php artisan make:migration create_yourtablename_table

如果您在该问题中粘贴了确切的代码,那么我会看到一个简单的拼写错误

email varchar(255) not n ull,

我知道我给出的愚蠢答案。 但您可以使用:

php artisan migrate --force
php artisan migrate:refresh
php artisan migrate:reset

最好使用这个。 可能会起作用:

php artisan migrate:rollback

请按照以下步骤进行迁移

检查数据库是否存在“用户”表出口,然后检查“ DROP用户” table

现在

步骤1.要创建迁移,请使用make:migration命令: php artisan make:migration create_yourtablename_table

步骤2.键入以下命令: php artisan migrate

采用

php artisan migrate:rollback

我有同样的问题,我所做的是注释掉用户 (up函数)中的行和password_reset迁移 ,然后我尝试了。 我不知道这是正确的方法! 如果我错了请纠正我

来自https://github.com/laravel/framework/issues/21100的答案。

您的问题可以通过更改create_users_table.php来解决

<?php

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

class CreateUsersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //Add this line
        Schema::dropIfExists('users');
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });
    }

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM