Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Migration table already exists, but I want to add new not the older

Tags:

php

mysql

laravel

I previously created users table. Now I have created a new migration to create a new books table inside my schema. When I try to run the command

php artisan migrate 

It shows:

[Illuminate\Database\QueryException] 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, `username` varchar(255) not null, `email` varchar(255) n ot null, `password` varchar(255) not null, `created_at` timestamp default 0  not null, `updated_at` timestamp default 0 not null) default character set  utf8 collate utf8_unicode_ci) 

Here is my new migration table:

<?php  use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;  class CreateBooksTable extends Migration {     public function up()     {         Schema::create('books', function(Blueprint $table)         {             $table->increments('id');             $table->string('name');             $table->string('auther');             $table->string('area');             $table->timestamps();         });     }      public function down()     {         Schema::drop('books');     } } 

How can I get rid of the error?

like image 868
MD. Atiqur Rahman Avatar asked Sep 27 '14 17:09

MD. Atiqur Rahman


People also ask

How do I add a column to an existing migration in Laravel?

The Laravel migrations will use the Schema facade to create and modify database tables and columns: Schema::create('tasks', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); }); Inside the facade, you could specify the different columns that you want to add.


2 Answers

In v5.x, you might still face the problem. So, try to delete related table manually first using

php artisan tinker

Then

Schema::drop('books')

(and exit with q)

Now, you can successfully php artisan migrate:rollback and php artisan migrate.

If this happens repeatedly you should check that the down() method in your migration is showing the right table name. (Can be a gotcha if you've changed your table names.)

like image 172
amrography Avatar answered Sep 18 '22 18:09

amrography


You need to run

php artisan migrate:rollback 

if that also fails just go in and drop all the tables which you may have to do as it seems your migration table is messed up or your user table when you ran a previous rollback did not drop the table.

EDIT:

The reason this happens is that you ran a rollback previously and it had some error in the code or did not drop the table. This still however messes up the laravel migration table and as far as it's concerned you now have no record of pushing the user table up. The user table does already exist however and this error is throw.

like image 41
eatingthenight Avatar answered Sep 16 '22 18:09

eatingthenight