代码之家  ›  专栏  ›  技术社区  ›  Famous Ighodaro

在laravel中迁移第二个表时出错

  •  0
  • Famous Ighodaro  · 技术社区  · 9 年前

    我跑步时有问题 php artisan migrate 在laravel中运行第一个users表之后,在我的第二个表上。当我运行第二个表迁移时,出现以下错误:

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

    4 回复  |  直到 9 年前
        1
  •  0
  •   Zayn Ali    9 年前

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

    您需要运行:

    php artisan migrate:refresh
    

    migrate 命令

        2
  •  0
  •   parker_codes    9 年前

    如果您刚开始使用Laravel Spark或使用了身份验证层,那么“users”表将给出该错误,因为它已经存在。仔细查看迁移,您应该会发现一个这样命名的表。

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

        3
  •  0
  •   Nikhil G    9 年前

    运行此 php artisan migrate:reset .

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

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

        4
  •  0
  •   Nitesh Kumar Niranjan    9 年前

    我已经解决了这个问题

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

    <?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');
        }
    }
    
    推荐文章