![](/img/trans.png)
[英]How to add decimal column to existing table in Laravel 5.3 migration
[英]how to alter an existing table column in laravel 5.3 using migrations
我有这个:
$ table-> integer('role_id')-> index()-> unsigned()-> nullable();
但我想将其更改为:
$ table-> integer('role_id')-> index()-> unsigned()-> nullable()-> default(3);
我曾经想使用它,但是我不明白的源语法:
PHP工匠make:migration update_role_id_in_users --table = users
我什至尝试使用doctrine/ddbal
软件包并运行以下命令:
PHP工匠make:migration Modify_role_id_in_users --table = users
迁移设置如下:
class ModifyRoleIdInUsers extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
//
$table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
//
$table->integer('role_id')->index()->unsigned()->nullable()->change();
});
}
}
但是当我进行迁移时,我得到了这个错误:
[Illuminate \\ Database \\ QueryException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'(SQL:alter table'users添加索引'users_role_id_index'('role_
ID'))[Doctrine \\ DBAL \\ Driver \\ PDOException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'
[PDOException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'
我如何在不进行migrate:refresh
情况下更改column
migrate:refresh
您可以使用change()
方法执行此操作:
$table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();
在通过迁移更新列之前,请记住运行
composer require doctrine/dbal
希望这可以帮助!
解决方案是:
default(3)
更改添加到role_id column
的末尾,例如$table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();
php artisan migrate
这就解决了,桌子也变了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.