我刚刚创建了一个新的迁移文件,将一个新列插入到现有表中。
文件代码为:
<?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()
{
//
}
}
但是当我运行php-artisan-migrate时,会出现一条错误消息:
[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)
有人知道问题出在哪里吗?
您以前的迁移角色仅半成功运行。这意味着表permission_role
已被添加,但提前终止。因此,迁移从未被标记为完成并添加到migrations
表中。它尝试重新运行"权限"迁移文件,但由于表已存在而失败。您需要手动将旧的迁移文件添加到数据库中,或者使其成功运行。
可以删除代码,也可以将其封装在if语句中
是否有任何方法可以检测Laravel 是否存在数据库表
确保该表在数据库中不存在,并且down()方法应该包含能够删除该表的代码。
在使用命令"php artisan migrate:refresh"
之前,您必须首先使用命令"php artisan migrate
"在迁移表中记录迁移原始文件,并且还需要更正down方法。
migrate:refresh命令将首先回滚所有数据库迁移,然后运行migrate命令。此命令有效地重新创建整个数据库:
php artisan migrate:refresh
php artisan migrate:refresh --seed
请参阅本文档。
将这行代码放入迁移中
if(!Schema::hasTable('post')){
Schema::create('post', function (Blueprint $table) {
$table->increments('id');
#etc
});
}