Php 尝试在laravel中重命名表的列会引发错误
我在修改Laravel中的列时遵循以下步骤 我有表Php 尝试在laravel中重命名表的列会引发错误,php,laravel,Php,Laravel,我在修改Laravel中的列时遵循以下步骤 我有表存储 并在命令行中运行此命令 php artisan make:migration rename\u stores\u column--table=“stores”--create 创建迁移后 这是密码 class RenameStoresColumn extends Migration { /** * Run the migrations. * * @return void */ publ
存储
并在命令行中运行此命令
php artisan make:migration rename\u stores\u 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
正确的方法是什么?您是否需要
原则/dbal
先决条件
在修改列之前,请确保将doctor/dbal依赖项添加到composer.json文件中。DBAL库用于确定列的当前状态,并创建对列进行指定调整所需的SQL查询:
您的问题似乎与此
rename\u stores\u列
迁移无关。看起来,php artisan migrate尝试重新运行create_stores_表
迁移,即使迁移已经运行
现在,如果在某个迁移过程中出现错误,您可能很容易遇到此问题,因为迁移运行时会创建表,但随后出现错误,批处理失败,并且无法在migrations
表中插入迁移的名称,因此在下一次迁移时会再次运行它,这就是你们这里的问题
要解决这个问题,您可以运行一个新的迁移(php-artisan-migrate:fresh
),但只有在您在开发环境中并且没有任何数据丢失的情况下才能运行
或者,在create\u stores\u table
migration中,将schema create
包装在schema has
中,因此即使它尝试运行它,如果该表已经存在,它也不会执行任何操作:
if (!Schema::hasTable('stores')) {
Schema::create('stores', function (Blueprint $table) {
$table->increments('id');
...
});
}
另一个选项是有多个迁移尝试创建(
Schema::create
)stores`表。你能验证一下吗?谢谢,伙计,它很有效,php artisan migrate:fresh无需担心。。我在开发环境中
if (!Schema::hasTable('stores')) {
Schema::create('stores', function (Blueprint $table) {
$table->increments('id');
...
});
}