14

规格:

  • Laravel 版本:5.5.3
  • PHP版本:7.1
  • 数据库驱动和版本:MariaDB 10.1.26

描述:

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

问题

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

4

21 回答 21

24

我通过更改我的 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');
    }
}
于 2017-09-13T16:31:50.590 回答
18

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

  1. 在我写的控制台中php artisan tinker

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

  3. 最后php artisan migrate,一切都奏效了。

于 2017-11-16T09:39:36.010 回答
13

解决此问题的简单方法是运行以下命令

php工匠迁移:新鲜

于 2018-11-25T11:15:16.727 回答
7

如果数据库中存在表,则可以通过添加以下代码跳过迁移表:

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();

    });
}
于 2020-04-03T20:35:27.240 回答
6

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

 1. php artisan tinker
 2. Schema::drop('your_table_name')
 3. php artisan migrate
于 2019-02-15T09:18:17.647 回答
3
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'),它会检查一个表是否已经存在于数据库中。如果您不想删除表,那么这是一个好主意。

于 2020-01-22T13:42:18.923 回答
2

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

  1. php artisan make:migration create_tableName_table --create=tableName.

  2. php工匠迁移。

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

  4. 创建新表为 1。

  5. 结束。好的。

于 2018-02-24T03:35:32.420 回答
1

有时我遇到和你一样的问题,在我检查之后是因为有时我懒得输入和复制粘贴来自 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();
    });

我忘了从这段代码中更改表名

Schema::create('users',

至此

Schema::create('what_ever_you_want_to_name_it',

我希望这会有所帮助

于 2021-01-11T04:30:09.033 回答
1

步骤1:

php artisan migrate:reset

第 2 步:使用 PHPmyadmin(或类似工具)访问您的数据库并删除所有剩余的表 - 包括迁移表。

第 3 步:

php artisan migrate
于 2021-09-17T14:59:39.510 回答
0

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

<?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');
}
}
于 2018-03-24T21:59:20.920 回答
0

只需先从数据库中删除这些列。并运行 composer update 。然后最后运行 php artisan migrate 它会解决你的问题。为您的问题提供最简单的解决方案。

于 2018-04-04T05:24:43.543 回答
0

如此链接所述,有两种可能的解决方案:

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

首先是回滚

php工匠迁移:回滚

其次是丢表。

于 2018-10-19T10:03:01.327 回答
0

解决方案:

  1. 如果您在本地主机上,请继续数据库 - > phpmyadmin
  2. 删除您创建的数据库上的所有内容
  3. 在 cmd 运行php artisan migrate
于 2019-03-03T09:05:59.623 回答
0

同意 Nitesh 的回答,但我认为这不是每次无缘无故丢掉桌子的最佳做法。根据文档,我们还可以使用带有条件 Schema::hasTable('users') 的简单“if”检查。所以我们可以将其用作:

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

当您已经有一个表并且需要一些修改然后您将能够在 else 语句中执行任何操作时,此解决方案也将是可行的。

于 2019-12-11T08:28:00.503 回答
0

使用时通常会出现迁移错误,php artisan migrate并且您的迁移有错误。

在 Laravel 中,有一些方法可以逆转已经执行的操作,尤其是迁移到更健壮的数据库,例如 MySql。

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

php artisan migrate

是运行

php artisan migrate: rollback

它会自动撤消上次执行的迁移您还可以使用 step 参数指定将撤消的步骤数。

数字表示将反转多少步

php artisan migrate: rollback --step=1
于 2020-01-26T17:36:07.530 回答
0

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

于 2020-04-28T04:13:15.530 回答
0

更改您的 Mysql 引擎设置

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

注意:这适用于 Laravel 5.2.14+

于 2020-06-27T08:09:00.783 回答
0

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

php artisan migrate:reset

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

php artisan migrate 

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

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

于 2021-03-09T08:29:32.760 回答
0

简单的解决方案

问题:

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

在这里,我的桌子有问题,Assign_Students您可能有任何问题

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

正如我从我的 MYSQL 中所做的那样,我 DROPAssign_Students然后我创建了我的新表示例:

php artisan make:model EmployeeSallaryLog -m

现在当你这样做时:

php artisan migrate`

你会发现两个表都像:

1) Assign_Students
2) EmployeeSallaryLog
于 2021-09-26T09:33:29.163 回答
0

对于 Laravel 8.0,之前的答案都不适合我。然后我在运行时注意到错误详细信息中的以下行php artisan migrate

加载存储的数据库模式:/Users/spedley/Sites/monkeys/database/schema/mysql-schema.dump

我删除了 mysql-schema.dump 文件,php artisan migrate再次运行它就可以了。

于 2021-10-17T17:24:51.723 回答
0

SQLSTATE [42S01]:基表或视图已存在:1050 表“用户”已存在(SQL:创建表usersidbigint unsigned not null auto_increment 主键,namevarchar(191) not null,emailvarchar(191) not null,email_verified_attimestamp null,passwordvarchar(191) not null, remember_tokenvarchar(100) null, created_attimestamp null, updated_attimestamp null) 默认字符集 utf8mb4 collat​​e 'utf8mb4_unicode_ci')

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

ID(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('密码'); $table->rememberToken(); $table->timestamps(); }); } /** * 反转迁移。* * @return void */ public function down() { Schema::dropIfExists('users'); } }
于 2022-01-15T07:03:10.613 回答