简体   繁体   中英

laravel 5 migration error when run newly created mirgration file

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?

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

确保该表实际上不在数据库中,并且down()方法应包含能够删除该表的代码。

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 .

put this line of code in your migrations

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM