Laravel 5.5 错误基础 table 或视图已存在:1050 Table 'users' 已存在

Laravel 5.5 Error Base table or view already exists: 1050 Table 'users' already exists

规格:

描述:

C:/Users/user/code/blog/>php artisan migrate

[Illuminate\Database\QueryException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null aut
o_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar
(100) null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci engine = InnoDB R
OW_FORMAT=DYNAMIC)

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

重现步骤:

 C:/Users/user/code/blog/>laravel new website

 C:/Users/user/code/blog/>php artisan make:migration create_lists_table --create=lists

 C:/Users/user/code/blog/>php artisan migrate

问题

它创建用户 table 并给出错误但不创建列表 table

我自己解决了我的问题 通过更改我的 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()
    {
        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');
    }
}

以下是我为解决同一问题所采取的步骤:

  1. 在控制台我写了php artisan tinker

  2. 然后,再次在控制台中,Schema::drop('users')

  3. 最后 php artisan migrate 一切都成功了。

以下是我为解决同一问题所采取的步骤:

  1. php artisan make:migration create_tableName_table --create=table名称.

  2. php artisan 迁移。

  3. 出现错误,您可以删除迁移中的所有文件和数据库中的所有table。

  4. 创建新的 table 作为 1.

  5. 完成。好的

我有不同的解决方案,我删除了迁移 table,这是我的解决方案

<?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()
{
    Schema::dropIfExists('migration');
    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');
}
}

首先从数据库中删除这些列。和 运行 作曲家更新。最后 运行 php artisan migrate 它会解决你的问题。针对您的问题的最简单的解决方案。

如本 link 所述,有两种可能的解决方案:

https://www.codespeaker.com/laravel-framework/solutions-for-common-errors-on-artisan-commands/

首先是回滚

php artisan migrate:rollback

其次是删除表。

解决这个问题的简单方法是运行下面的命令

php artisan migrate:fresh

以下命令解决了我的问题:

 1. php artisan tinker
 2. Schema::drop('your_table_name')
 3. php artisan migrate

解决方案:

  1. 继续数据库 -> phpmyadmin 如果你在 localhost
  2. 删除您创建的数据库中的所有内容
  3. 在命令上运行php artisan migrate

同意 Nitesh 的回答,但我认为每次无缘无故地删除 table 并不是最佳做法。我们还可以根据 docs 使用简单的 "if" 检查条件 Schema::hasTable('users')。所以我们可以将其用作:

<?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()
    {
        if(!Schema::hasTable('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');
    }
}

如果您已经有一个 table 并且需要一些修改,那么您也可以在 else 语句中执行任何操作。

if (!Schema::hasTable('users')) {
            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();
            });
        }

使用 !Schema::hasTable('users') ,它会检查 table 是否已经存在于数据库中。 如果您不想删除 table 那么这是个好主意。

迁移错误通常发生在使用php artisan migrate并且您的迁移出现错误时。

Within Laravel, there are some ways to reverse something that has been performed, especially migrations to more robust databases like MySql, for example.

一种逆转所采取步骤的方法

php artisan migrate

是对运行的

php artisan migrate: rollback

它会自动撤销上次执行的迁移 您还可以使用 step 参数委托将反转多少步。

The number says how many steps will be reversed

php artisan migrate: rollback --step=1

如果数据库中存在 table,您可以跳过迁移 table,方法是添加以下代码:

public function up()
{
    if(Schema::hasTable('users')) return;       //add this line to your database file

    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->timestamps();

    });
}

通过命令创建迁移 php artisan make:migration create_category_table 然后在你的 database/migrations 中设置你必要的字段和数据库,然后 运行 这个命令 pho artisan 迁移 --pretend 它将显示 sql 语句,复制 sql 类别语句并将其粘贴到 sql 中的数据库中,只需单击 运行,您的迁移就在那里,无需删除用户 table

更改您的 Mysql 引擎设置

config/database.php 中,将您的 mysql 引擎设置更改为: 'engine' => 'innodb row_format=dynamic'.

注意:这适用于Laravel 5.2.14+

有时我遇到和你一样的问题,在我检查之后是因为有时我懒得输入和复制粘贴来自 create_user_table.php

的代码
Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

而且我忘记更改此代码中的 table 名称

Schema::create('users',

致此

Schema::create('what_ever_you_want_to_name_it',

希望对您有所帮助

您可以使用此命令重置所有内容

php artisan migrate:reset

然后你可以运行这个命令。

php artisan migrate 

记住重置会删除您的用户 table。请不要使用手动方法删除 table 或对数据库进行任何操作。 Laravel 允许我们使用命令来做任何事情,这就是它们的美妙之处。如果您想了解有关使用 Laravel 迁移的详细信息,请查看此 link.

https://laramatic.com/how-to-use-migrations-in-laravel-code-example/

第 1 步:

php artisan migrate:reset

第 2 步: 使用 PHPmyadmin(或类似工具)转到您的数据库并删除所有剩余的 tables - 包括迁移 table.

第 3 步:

php artisan migrate

简单的解决方案

问题:

SQLSTATE[42S01]: Base table or view already exists: 1050 Table Assign_Students already exists.

这里我遇到了问题 Assign_Students table 你可能有

X Y Z Table
ANS: DROP YOUR X Y Z Table from your SQLDB

正如我在 MYSQL 中所做的那样,我删除了 Assign_Students,然后创建了新的 table 示例:

php artisan make:model EmployeeSallaryLog -m

现在当你这样做时:

php artisan migrate`

你会发现两个 table 都像:

1) Assign_Students
2) EmployeeSallaryLog

对于 Laravel 8.0,none 较早的答案对我有用。然后我在 运行 php artisan migrate:

时注意到错误详细信息中的以下行

Loading stored database schema: /Users/spedley/Sites/monkeys/database/schema/mysql-schema.dump

我删除了 mysql-schema.dump 文件,再次 运行 php artisan migrate 并且成功了。

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id bigint unsigned not null auto_increment 主键,name varchar(191) not null,email varchar(191) not null,email_verified_at timestamp null, password varchar(191) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp null) 默认字符集 utf8mb4整理 'utf8mb4_unicode_ci')

下面的代码就像魔术一样。它运行良好。

ID(); $table->字符串('name'); $table->string('email')->unique(); $table->时间戳('email_verified_at')->可空(); $table->字符串('password'); $table->rememberToken(); $table->时间戳(); }); } /** * 反转迁移。 * * @return无效 */ public 函数向下() { Schema::dropIfExists('users'); } }