Reputation: 169
I am new to laravel. I have created migrations and I can see them in my database/migrations folder. But when I run the php artisan migrate
command, it throws this error:
[PDOException]
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists
Yes, I have already migrated the user table already, but now I have created another migration. Isn't it supposed to migrate the last migration I have created after my php artisan migrate
command?
Upvotes: 0
Views: 69
Reputation: 6392
If your second migration for the users table looks like this:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name', 25);
// more migrations ...
});
it will try to create the users
table again. Note the second migration has Schema::
create. You are looking to do something more like alter the table:
Schema::table('users', function (Blueprint $table) {
$table->string('name', 50)->change();
});
If you are looking to completely overwrite (this will delete all data in the table) your prior migration with a new table you could do something as follows:
<?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', 50);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
However, if you are doing this you are most likely misunderstanding how migrations work. The purpose is to be able to alter columns in your database without having to completely destroy the table.
Upvotes: 1