0
votes

I've created a table using command

create table: php artisan make:migration create_movie --create=movie

then added body & user_id columns to code

public function up()
{
    Schema::create('movie', function (Blueprint $table) {

        $table->increments('id');

        $table->text('body');

        $table->integer('user_id');

        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('movie');
}

then hit php artisan migrate command

but this is showing me this exception and I'm not able to add movie table to the database

[Illuminate\Database\QueryException] SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'users' already exists (SQL: create table users ( id int unsigned not null auto_increment primary key, name varchar(255) not null, email varchar(255) not null, password varchar(255) not null, remember_token varchar(100) null, created_at timestamp null, updated_at tim estamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

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

4
The problem maybe is not in this migration ? Seems like you tray adds users twice. Maybe tray run "composer dumpautoload" - napalias
Maybe you dropped or truncate your migrations table? It seems the problem is when trying to create (or re-create) users table, not this movie table. - fmgonzalez
Seems like users table is not added with artisan command but still exists. If you don't care of data in tables (careful, this would delete all tables) try with php artisan migrate:fresh or php artisan migrate:refresh commands. Or you can delete tables manually than issue php artisan migrate command. - Tpojka

4 Answers

0
votes

Empty database tables and run migration again or if you do not want to lose your data do the follow:

Find your movie table migration (Database>Migrations) and change the date to any oldest date on the migration file so that it goes first in the folder.

Run migration again, even if you get error about users table (its not error, its just saying that it already exist) the movie table will be created.

Migration migrates from top to bottom

0
votes

You can try run new generate movie specific file migration like this

php artisan migrate --path=/database/migrations/{{Your_movie_migration_filename}}
0
votes

Please empty migrations table and run php artisan migrate command

OR

Create test folder in migrations folder then newly created migration moved/copied in test folder and run below command in your terminal/cmd like:

php artisan migrate --path=/database/migrations/test/
0
votes

You can use the next artisan command that will roll back all of your migrations and then execute the migrate command. Please, check de documentation here https://laravel.com/docs/8.x/migrations#roll-back-migrate-using-a-single-command

php artisan migrate:fresh