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

徘徊边缘 提交于 2019-12-02 03:30:44

问题


Here my migration table

    <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserTable extends Migration
{

    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->unsigned();
            $table->string('name');
            $table->string('email',50)->unique();
            $table->string('username');
            $table->string('password');
            $table->boolean('active');
            $table->rememberToken();
            $table->timestamps();

        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}

when ever i try to migrate the following error occur

*[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre ady exists (SQL: create table users (id int unsigned not null auto_incr ement primary key, role_id int unsigned not null, name varchar(191) not null, email varchar(50) not null, username varchar(191) not null, pas sword varchar(191) not null, active tinyint(1) not null, remember_token varchar(100) null, created_at timestamp null, updated_at timestamp nu ll) default character set utf8mb4 collate utf8mb4_unicode_ci)

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

please tell me what should i do ?? i have already use migrate:reset or dumpautoload or rollback nothing happen . lots of time i edit or delete this usersfile and recreated it .


回答1:


Try like this

   <?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUserTable extends Migration
{

    public function up()
    {
        Schema::dropIfExists('users');
        Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('role_id')->unsigned();
            $table->string('name');
            $table->string('email',50)->unique();
            $table->string('username');
            $table->string('password');
            $table->boolean('active');
            $table->rememberToken();
            $table->timestamps();

        });
    }

    public function down()
    {
        Schema::dropIfExists('users');
    }
}



回答2:


this problem is showing when you want migrate tables that exist in database : for this problem just go to PHPMYADMIN manually and drop all table and now run command php artisan migrate




回答3:


DANGER - by dropping a table, rolling back or refreshing your database will destroy all data in that table or database and these methods are not recommended for production use and you should definitely not include drop table behavior in your migration.

It's clear there are a lot of "solutions" for this problem, but all the solutions I read through are very destructive solutions and none of them work for a production database. Yes, they get rid of the error, but at what expense?

Based on the number of solutions, it also seems that there could be several causes to this error.

My error was caused by a missing entry in my migrations table. Which required a very simple solution -- adding the entry back in. A simple issue like this definitely doesn't warrant a database refresh



来源:https://stackoverflow.com/questions/44141475/sqlstate42s01-base-table-or-view-already-exists-or-base-table-or-view-already

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!