4

I wanted to make a migration using php artisan migrate but I got an error:

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

So naturally I wanted to drop my tables using php artisan migrate:rollback, but I got:

Nothing to rollback.

What the heck? It said just earlier that the users table was already created. What should I do? I can delete the table with phpmyadmin but I want to learn how to use artisan.

Thank you for your help.

JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61
  • What if you use `php artisan migrate:reset` instead? – Troyer Apr 23 '18 at 14:35
  • You can only rollback the last migration executed. Is there any last migration that can be rolled back? What is the output of `php artisan migrate:status`? Do you see any migrations in the `migrations` table from the database? If so, are those migrations what are you expecting them to be? – Bogdan Apr 23 '18 at 14:39
  • Sorry, Stack Overflow was on maintenance for a couple of minutes and I tried to manage it myself. What I did is delete all the tables (even the migration one) using phpmyadmin, and then I fixed what caused the fact that I had to make a `php artisan migrate:rollback` in the first place, I added `Schema::defaultStringLength(191);` in AppServiceProvider. Now it seems to work well. – JacopoStanchi Apr 23 '18 at 14:48
  • Write the solution as an answer. – Troyer Apr 23 '18 at 14:50
  • @Troyer That's done. – JacopoStanchi Apr 23 '18 at 15:04

5 Answers5

3

Failing an answer to how to delete the tables with artisan when there is this problem, I will tell you how to avoid this problem in the first place. Ultimately I just deleted the tables using phpmyadmin.

I watched a tutorial on Laravel and I saw that in order to avoid the error:

SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes

I had to add use Illuminate\Support\Facades\Schema; in AppServiceProvider.php and Schema::defaultStringLength(191); in the boot() function. But I thought that I could put any number in defaultStringLength(...) so I put 255, and the migration didn't work.

Do as it is told in the tutorials, write Schema::defaultStringLength(191);. Then, you can execute your migration using php artisan migrate and it should work properly.

JacopoStanchi
  • 1,962
  • 5
  • 33
  • 61
2

In my case after defining Schema::defaultStringLength(191); in the boot() method didn't solved. Because the migrations table in the database is empty. So the solution is open up the tinker from the composer

$ php artisan tinker
>>> Schema::drop('users')
>>> Schema::drop('password_resets')
>>> Schema::drop('orders')
>>> exit
php artisan migrate

Here is the result of the above commands executed

nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate

In Connection.php line 647: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users (id int unsigned not null auto_incr ement primary key, name varchar(255) not null, email varchar(255) not n ull, 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)

In Connection.php line 449: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' alre ady exists

nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate:rollback
Nothing to rollback.
nishanth@localhost:~/Desktop/html/hutch$ php artisan tinker
Psy Shell v0.8.17 (PHP 7.1.20-1+ubuntu16.04.1+deb.sury.org+1 — cli) by Justin Hileman
>>> Schema::drop('users')
=> null
>>> Schema::drop('password_resets')
=> null
>>> Schema::drop('orders')
=> null
>>> exit
Exit:  Goodbye.
nishanth@localhost:~/Desktop/html/hutch$ php artisan migrate
Migrating: 2014_10_12_000000_create_users_table
Migrated:  2014_10_12_000000_create_users_table
Migrating: 2014_10_12_100000_create_password_resets_table
Migrated:  2014_10_12_100000_create_password_resets_table
Migrating: 2018_08_18_071213_create_orders_table
Migrated:  2018_08_18_071213_create_orders_table
nishanth@localhost:~/Desktop/html/hutch$ 

Also define the method down(), if it doesn't exist.
Otherwise, it'll show

SQLSTATE[42S02]: Base table or view not found: 1051 Unknown table 'XYZ.ABC' (SQL: drop table ABC)

/**
  * Reverse the migrations.
  *
  * @return void
  */
public function down()
{
    Schema::dropIfExists('ABC');
}
Nɪsʜᴀɴᴛʜ ॐ
  • 2,756
  • 4
  • 33
  • 57
  • Beware, Tkinter and Tinker are not the same thing XD But you're right though, it was because my `migrations` table was empty. – JacopoStanchi Aug 20 '18 at 14:08
2

SOLUTION:

>> php artisan migrate:fresh

It will drop all tables and recreates all the migrations.

Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43
  • 1
    Your answer is good if we just want to delete the tables, but we must also correct the problem of the table that is created by Artisan but that does not appear in the migration table when we use `defaultStringLength(255)`. I don't know if it's a bug of Artisan but that's the core problem. – JacopoStanchi Jan 19 '19 at 12:12
  • I am not sure if you are concerned about this issue too. https://stackoverflow.com/a/54265587/5276297 – Naveen Kumar V Jan 19 '19 at 12:19
0
  1. Add use Illuminate\Support\Facades\Schema; in AppServiceProvider.php and Schema::defaultStringLength(191); in the boot() function
  2. Run php artisan migrate:fresh
  3. Run php artisan migrate again

Solved!

S. M. JAHANGIR
  • 4,324
  • 1
  • 10
  • 30
0

php artisan db:wipe

It is a quick way to drop all the tables, their types and views if you’re using Laravel 6.x

Full description:

$ php artisan db:wipe {--database=} {--drop-views} {--drop-types} {--force}

  • database - The database connection to use
  • drop-views - Drop all tables and views
  • drop-types - Drop all tables and types (Postgres only)
  • force - Force the operation to run when in production