2

i just have created a new migration file, to insert a new column to existing table.

the file code is:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddStatusToPhoto extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('photos', function(Blueprint $table)
        {
            $table->integer('status');
        });

    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }


}

but when i run php artisan migrate, there is an error message:

  [Illuminate\Database\QueryException]
  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'permission_
  role' already exists (SQL: create table `permission_role` (`id` int unsigne
  d not null auto_increment primary key, `permission_id` int unsigned not nul
  l, `role_id` int unsigned not null, `created_at` timestamp default 0 not nu
  ll, `updated_at` timestamp default 0 not null) default character set utf8 c
  ollate utf8_unicode_ci)

anyone know what is the problem?

hkguile
  • 4,235
  • 17
  • 68
  • 139

4 Answers4

0

Your previous migration role ran only semi-successfully. This means that the table permission_role got added, but terminated early. Because of that, the migration never got marked as complete and added to the migrations table. It tries to re-run the Permissions migration file, but fails as the table already exists. You either need to add the old migration file to the database manually, or make it so it runs successfully.

It is possible to remove the code, or wrap it in an if statement.
Is there any way to detect if a database table exists with Laravel

Community
  • 1
  • 1
Josh
  • 3,258
  • 2
  • 19
  • 31
0

Make sure the table did not physically exists in the database and down() method should contain code that be able to drop that table.

0

You should have to record first the migration raw file in migrations table by using the command "php artisan migrate" before using the command "php artisan migrate:refresh" and also you need to correct the down method.

The migrate:refresh command will first roll back all of your database migrations, and then run the migrate command. This command effectively re-creates your entire database:

php artisan migrate:refresh

php artisan migrate:refresh --seed

Refer this documentation.

Ravi Hirani
  • 6,511
  • 1
  • 27
  • 42
  • i'had tried refresh but not work(and also i remember will delete all data?), finally i delete all old migration file temporarily – hkguile May 04 '16 at 05:47
0

put this line of code in your migrations

if(!Schema::hasTable('post')){
  Schema::create('post', function (Blueprint $table) {
            $table->increments('id');
            #etc
        });
}
muha
  • 27
  • 1
  • 6