尝试在laravel中重命名表的列会引发错误

时间:2018-05-02 06:47:31

标签: php laravel

我在修改Laravel中的列时遵循guide here

我有桌子stores 并在命令行中运行

php artisan make:migration rename_stores_column --table="stores" --create

创建迁移后

这里是代码

class RenameStoresColumn extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('stores', function (Blueprint $table) {
          $table->renameColumn('store_iamge', 'store_image');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('stores', function (Blueprint $table) {
          $table->renameColumn('store_image', 'store_iamge');
        });
    }
}

但是当我运行php artisan migrate

我收到了这个错误,

In Connection.php line 664:

  SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'stores' already exists (SQL: create table `stores` (`id` int unsigned not null auto_increment primary key, `name` varchar(255) null, `address` varchar(255) null, `city` varchar(255) null, `zipcode` varc
  har(255) null, `country` varchar(255) null, `state` varchar(255) null, `latitude` double(10, 6) not null, `longitude` double(10, 6) not null, `description` text null, `phone` varchar(255) null, `email` varchar(255) null, `fax` varchar(255) null, `web` varchar(255) n
  ull, `tags` varchar(255) null, `schedule` varchar(255) null, `store_iamge` varchar(255) null, `marker_image` varchar(255) null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8 collate utf8_unicode_ci)


In PDOStatement.php line 143:

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

正确的方法是什么?

2 个答案:

答案 0 :(得分:2)

您需要doctrine/dbal吗?

  

<强>先决条件

     

在修改列之前,请务必将doctrine / dbal依赖项添加到composer.json文件中。 Doctrine DBAL库用于确定列的当前状态,并创建对列进行指定调整所需的SQL查询:

composer require doctrine/dbal

答案 1 :(得分:1)

您的问题似乎与此var src = './clients/*/assets/less/*.less'; return gulp.src(src,{base: './clients/'}) .pipe(plumber()) .pipe(sourcemaps.init()) .pipe(less({ paths:[path.join(__dirname, 'less')] }).on('error', function(err){ gutil.log(err); this.emit('end'); })) .pipe(cleanCSS({debug: true}, function(details) { console.log(details.name + ': ' + details.stats.originalSize); console.log(details.name + ': ' + details.stats.minifiedSize); })) .pipe(filesize()) .pipe(rename(function (path) { path.dirname = path.dirname.replace(/less/i,'css'); })) .pipe(gulp.dest('./clients/')) .pipe(notify({ message: "Style compressed", onLast: true}) ); 迁移无关。看起来rename_stores_column尝试重新运行php artisan migrate迁移,即使它已经运行了。

现在您可能很容易遇到此问题,如果您的某个迁移中有任何错误,因为迁移运行,它会创建表,但随后发生错误,批处理失败并且无法管理在create_stores_table表中插入迁移的名称,因此在下次迁移时它将再次运行它,因此您遇到此问题。

要解决此问题,您只能在开发环境中运行全新迁移(migrations)但,并且没有任何数据丢失

或者,在php artisan migrate:fresh迁移中将create_stores_table包裹在schema create中,所以即使它尝试运行它,如果该表已经存在,它也不会执行任何操作:

schema has
相关问题