Laravel won't let me migrate a table because it already exists
In laravel 5.5 and > you can use:
$ php artisan migrate:fresh
// ( not migrate:refresh wich is a different command)
This command first drops all tables and then reruns all migrations
I had neither removed the migration entry nor dropped the table manually from the database In my case, 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' alre ady exists (SQL: create table
users
(id
int unsigned not null auto_incr ement primary key,name
varchar(255) not 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)In Connection.php line 449: SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already 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');
}
Try this,
Add following line :Schema::dropIfExists('mytable');
inside the Up() function at the very beggining exactly before creating schema for the mytable.
i.e. before following code. Schema::create('mytable', function (Blueprint $table)