【问题标题】:PHP artisan migrate command giving errors?PHP工匠迁移命令给出错误?
【发布时间】:2017-07-29 08:33:25
【问题描述】:

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

[Illuminate\Database\QueryException] SQLSTATE[42S01]:基表或 视图已存在:1050 表“用户”已存在(SQL:创建 表users (id int unsigned not null auto_increment 主键, name varchar(255) 不为空,email varchar(255) 不为空, password varchar(255) 不为空,remember_tokenvarchar(100) 为空, created_attimestamp null, updated_attimestamp null) 默认 字符集 utf8mb4 整理 utf8mb4_unicode_ci)

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

我在 Windows 上使用 Laravel 5.4。

【问题讨论】:

标签: php mysql laravel


【解决方案1】:

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');
    }
}

【讨论】:

    【解决方案2】:

    我遇到了同样的问题,我所做的是在 users 和 password_reset migration 中将 (up function) 中的行注释掉,然后我尝试了它。我不知道这是否是正确的做法!如果我错了,请纠正我

    【讨论】:

      【解决方案3】:

      使用

      php artisan migrate:rollback
      

      【讨论】:

        【解决方案4】:

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

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

        你可以运行:

        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
        

        【讨论】:

          【解决方案5】:

          按照这些步骤到migrate

          检查你的数据库 如果存在 'users' 表退出,则 DROPuserstable

          现在

          第 1 步。 要创建迁移,请使用 make:migration 命令:-php artisan make:migration create_yourtablename_table

          第 2 步。 输入此命令:php artisan migrate

          【讨论】:

            【解决方案6】:

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

            email varchar(255) not n ull,
            

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

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

            更好地使用它。可能它会起作用:

            php artisan migrate:rollback
            

            【讨论】:

              猜你喜欢
              • 2017-06-20
              • 2016-06-16
              • 1970-01-01
              • 2017-11-29
              • 1970-01-01
              • 1970-01-01
              • 2016-05-17
              • 2017-11-29
              • 2014-06-01
              相关资源
              最近更新 更多