【问题标题】:Laravel 5.5 Error Base table or view already exists: 1050 Table 'users' already existsLaravel 5.5 错误基表或视图已存在:1050 表“用户”已存在
【发布时间】:2018-02-18 03:14:45
【问题描述】:

规格:

  • 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

问题

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

【问题讨论】:

  • 清除您的数据库并重试
  • @apokryfos 我删除了所有的表和数据库,但它给出了同样的错误
  • 您可以将其发布为您问题的答案吗?它可能会在将来帮助其他有类似问题的人
  • 如果您没有重复条目,请签入迁移文件。也许您已经复制了用户架构并忘记对其进行编辑。如果是这样,Laravel 会看到您尝试输入“用户”模式两次。

标签: php mysql laravel-5 laravel-5.5


【解决方案1】:

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

【讨论】:

  • 也可以,不过laravel有更简单的形式可以解决问题
【解决方案2】:

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

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

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

  3. 最后php artisan migrate 一切正常。

【讨论】:

    【解决方案3】:

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

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

    2. php artisan 迁移。

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

    4. 将新表创建为 1。

    5. 完成。好的。

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        此链接中提到了两种可能的解决方案:

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

        首先是回滚

        php 工匠迁移:回滚

        其次是删除表。

        【讨论】:

          【解决方案7】:

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

          php artisan migrate:fresh

          【讨论】:

          • @Florin 它还会再次创建表
          • 请注意,这将从头开始创建所有内容,您将丢失数据。
          • @Rajat:这就是为什么人们不应该盲目地使用在这个站点或任何其他站点上找到的代码,而应该发现代码背后的想法,然后根据获得的知识创建自己的代码。否则,您使用代码“风险自负”。最好放置一个kind评论提及您的问题,否决答案(如果合适),然后继续。
          【解决方案8】:

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

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

          【讨论】:

            【解决方案9】:

            解决办法:

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

            【讨论】:

              【解决方案10】:

              同意 Nitesh 的回答,但我认为这不是每次无缘无故丢表的最佳做法。根据docs,我们还可以使用带有条件 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 语句中执行任何操作时,此解决方案也是可行的。

              【讨论】:

              • 那时我还不太了解。那时我还只是一个 13 岁的孩子,我就开始编程了。并试图回答我被禁止回答的问题。
              【解决方案11】:
              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') ,它会检查一个表是否已经存在于数据库中。 如果您不想删除表,那么这是一个好主意。

              【讨论】:

                【解决方案12】:

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

                在 Laravel 中,有一些方法可以扭转已经发生的事情 执行,尤其是迁移到更强大的数据库,如 MySql, 例如。

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

                php artisan migrate
                

                是运行

                php artisan migrate: rollback
                

                它会自动反转上次执行的迁移 您还可以使用 step 参数指定将反转多少个步骤。

                数字表示将反转多少步

                php artisan migrate: rollback --step=1
                

                【讨论】:

                  【解决方案13】:

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

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

                  【讨论】:

                    【解决方案14】:

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

                    【讨论】:

                      【解决方案15】:

                      更改您的 Mysql 引擎设置

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

                      注意:这适用于 Laravel 5.2.14+

                      【讨论】:

                        【解决方案16】:

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

                        希望对你有帮助

                        【讨论】:

                          【解决方案17】:

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

                          php artisan migrate:reset
                          

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

                          php artisan migrate 
                          

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

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

                          【讨论】:

                            【解决方案18】:

                            第 1 步:

                            php artisan migrate:reset
                            

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

                            第 3 步:

                            php artisan migrate
                            

                            【讨论】:

                              【解决方案19】:

                              简单的解决方案

                              问题:

                              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 中所做的那样,我 DROP Assign_Students 然后我创建了我的新表示例:

                              php artisan make:model EmployeeSallaryLog -m
                              

                              现在当你这样做时:

                              php artisan migrate`
                              

                              你会发现这两个表都像:

                              1) Assign_Students
                              2) EmployeeSallaryLog
                              

                              【讨论】:

                                【解决方案20】:

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

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

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

                                【讨论】:

                                  【解决方案21】:

                                  SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在(SQL:创建表 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 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 无效 */ 公共函数向下() { Schema::dropIfExists('users'); } }

                                  【讨论】:

                                    猜你喜欢
                                    • 1970-01-01
                                    • 1970-01-01
                                    • 2022-07-09
                                    • 1970-01-01
                                    • 2021-04-08
                                    • 2016-01-28
                                    • 2018-05-23
                                    • 2019-12-30
                                    • 2019-03-05
                                    相关资源
                                    最近更新 更多