【问题标题】:I having error migrating my second table in laravel我在 laravel 中迁移第二个表时出错
【发布时间】:2018-01-20 23:08:04
【问题描述】:

在 laravel 中运行第一个用户表后,我在第二个表上运行 php artisan migrate 时遇到问题。当我运行第二个表迁移时,我收到此错误:

[PDO异常] SQLSTATE[42S01]:基表或视图已存在:1050 表“用户”已存在

【问题讨论】:

  • 对于那些因为“迁移”表没有自动创建而最终来到这里的人,这个link 可能会有所帮助。

标签: php laravel web laravel-5.3


【解决方案1】:

如果你开始使用 Laravel Spark 或者你已经使用了身份验证层,那么“用户”表会给你这个错误,因为它已经存在。查看您的迁移,您应该找到一个这样命名的表。

另一种可能性是您没有正确设置环境变量,并且您正在使用与另一个项目相同的数据库。如果之前的项目有一个“用户”表,那么也会显示这个错误。

【讨论】:

    【解决方案2】:

    运行这个php artisan migrate:reset

    下次要添加另一个表时,需要先检查该表是否已经存在。您可以使用

    if(!Schema::hasTable('users')){
        // Write your schema create code here
    }
    

    保存文件,然后运行 ​​php artisan migrate 命令。

    【讨论】:

      【解决方案3】:

      php artisan migrate 运行 database/migrations 文件夹中的所有迁移。因此,首先您迁移了 users 表,然后您创建了一个新的第二个表迁移并运行此命令,因此 laravel 尝试再次迁移已存在于您的数据库中的 users 迁移并抛出此错误。

      你需要运行:

      php artisan migrate:refresh
      

      它将回滚您的所有迁移,然后执行migrate 命令。

      【讨论】:

      • 感谢您的回复,但它仍然不适合我,即使我运行了
      • 请手动从数据库中删除users 表,然后重试。
      • 删除用户表后它现在正在工作,但在这种情况下,我的练习项目中也需要用户表。 @zayn 阿里
      • @FamousIghodaro 是的,因为 laravel 还在数据库中创建了一个 migrations 表来跟踪所有数据。
      【解决方案4】:

      我已经解决了这个问题

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

      通过更改我的 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-02-21
        • 1970-01-01
        • 2018-05-03
        • 2020-06-22
        • 1970-01-01
        • 2016-04-02
        • 2019-05-30
        • 2021-02-21
        • 1970-01-01
        相关资源
        最近更新 更多