3

I get this error when I want to php artisan migrate. The error message is as follows

   Illuminate\Database\QueryException

SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id bigint unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, email_verified_at timestamp 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')

at C:\composer\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:671 667▕ // If an exception occurs when attempting to run a query, we'll format the error 668▕ // message to include the bindings with SQL, which will make this exception a 669▕ // lot more helpful to the developer instead of just the database's errors. 670▕ catch (Exception $e) { ➜ 671▕ throw new QueryException( 672▕ $query, $this->prepareBindings($bindings), $e 673▕ ); 674▕ } 675▕

1
C:\composer\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464 PDOException::("SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists")

2
C:\composer\blog\vendor\laravel\framework\src\Illuminate\Database\Connection.php:464 PDOStatement::execute()

And Database settings are as follows. The settings in the Database.php file are as follows

 'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', 'localhost'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'laravel'),
            'username' => env('DB_USERNAME', 'root'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

The settings in the .env file are as follows

APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:sBKduFaKhOJdg7/A1U4IzAUnj3yLLcjngjmMvEoWl94=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=

Note: I use mysql for wampserver.

4
  • 1
    Welcome to SO .. 1050 Table 'users' already exists" can you see error it is saying users already exites so can't create again or you can try php artisan migrate:fresh Commented Sep 24, 2020 at 10:48
  • I've already tried this, nothing changes. Keeps giving error Commented Sep 24, 2020 at 10:51
  • manfully delete users table Commented Sep 24, 2020 at 10:54
  • php artisan db:wipe (it will drop all tables from db), and then php artisan migrate Commented Sep 24, 2020 at 12:59

5 Answers 5

2

remove already migrated files, then execute migration command

Sign up to request clarification or add additional context in comments.

Comments

2

Here are the steps i took to solve the same issue like:

  1. In the console i wrote => php artisan tinker

  2. Then again in console => Schema::drop('users')

  3. At the end => php artisan migrate

2 Comments

When it does what you say, it gives this error. SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table users add unique users_email_unique(email))
Update your /app/Providers/AppServiceProvider.php to contain: use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); }
0

maybe below command will help!

php artisan migrate:fresh

migrate command and their uses

  migrate:fresh        Drop all tables and re-run all migrations
  migrate:install      Create the migration repository
  migrate:refresh      Reset and re-run all migrations
  migrate:reset        Rollback all database migrations
  migrate:rollback     Rollback the last database migration
  migrate:status       Show the status of each migratioin

1 Comment

Could you expand you answer to include what this command does?
0

just go to php path in windows you will find it here

Path to php in windows
C:\tools\php82\php.ini

then uncomment whatever you want for php extensions for me i uncommented pdo_mysql and sqlite and worked fine

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
0

To fix this error, you should update AppServiceProvider.php.

use Illuminate\Support\Facades\Schema;

public function boot(): void {
    Schema::defaultStringLength(191); 
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.