[英]how to alter an existing table column in laravel 5.3 using migrations
I have this: 我有这个:
$table->integer('role_id')->index()->unsigned()->nullable();
$ table-> integer('role_id')-> index()-> unsigned()-> nullable();
But I want to change it to this: 但我想将其更改为:
$table->integer('role_id')->index()->unsigned()->nullable()->default(3);
$ table-> integer('role_id')-> index()-> unsigned()-> nullable()-> default(3);
I had wanted to use this, but the source syntax I could not understand: 我曾经想使用它,但是我不明白的源语法:
php artisan make:migration update_role_id_in_users --table=users
PHP工匠make:migration update_role_id_in_users --table = users
I even tried using doctrine/ddbal
package and running this: 我什至尝试使用
doctrine/ddbal
软件包并运行以下命令:
php artisan make:migration modify_role_id_in_users --table=users
PHP工匠make:migration Modify_role_id_in_users --table = users
with the migration set up like this: 迁移设置如下:
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();
});
}
}
But I get this error when I go migrate: 但是当我进行迁移时,我得到了这个错误:
[Illuminate\\Database\\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'users_role_id_index' (SQL: alter table 'users' add index 'users_role_id_index'('role_
[Illuminate \\ Database \\ QueryException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'(SQL:alter table'users添加索引'users_role_id_index'('role_
id'))ID'))
[Doctrine\\DBAL\\Driver\\PDOException] SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'users_role_id_index'
[Doctrine \\ DBAL \\ Driver \\ PDOException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'
[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name 'users_role_id_index'
[PDOException] SQLSTATE [42000]:语法错误或访问冲突:1061重复的键名'users_role_id_index'
How can I alter the column
without doing a migrate:refresh
我如何在不进行
migrate:refresh
情况下更改column
migrate:refresh
You can use change()
method to do this: 您可以使用
change()
方法执行此操作:
$table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();
Remember to run
composer require doctrine/dbal
before updating your columns via migrations在通过迁移更新列之前,请记住运行
composer require doctrine/dbal
Hope this helps! 希望这可以帮助!
The solution was to: 解决方案是:
default(3)
to the end of the role_id column
like this $table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();
default(3)
更改添加到role_id column
的末尾,例如$table->integer('role_id')->index()->unsigned()->nullable()->default(3)->change();
php artisan migrate
php artisan migrate
And that solved it and the table was altered. 这就解决了,桌子也变了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.