使用laravel migration Migrations#Modifying Columns中的数据更改列中的数据类型

问题描述 投票:1回答:3

这是迁移?我必须将字符串数据列更改为具有现有数据的整数数据列

public function up()
{
       Schema::table('SYS_tenants' ,function (Blueprint $table){
           $table->integer('tena_type')->unsigned()->nullable()->change();
           $table->foreign('tena_type')->references('id')->on('account_types');
       });
}

laravel database-migration
3个回答
2
投票

根据laravel文档,您可以创建一个新的迁移并按以下步骤进行:

Schema::table('SYS_tenants', function (Blueprint $table) {
   $table->integer('tena_type')->unsigned()->nullable()->change();
});

在修改列之前,请确保添加doctrine/dbal依赖项到您的composer.json文件。

composer require doctrine/dbal

参考:Laravel -> Database: Migrations-> Modifying Columns


0
投票

您可以在设置新字段类型后在要更改类型的字段上使用change方法。

public function up() {
    Schema::table('SYS_tenants' ,function (Blueprint $table){

        $table->string('tena_type')->change();   
    });
}

我以为创建表的迁移已经调用了您需要的所有要求,例如uniquenullable等。您可以调用change方法,对您要执行的任何修改都没有限制,例如在该字段上添加其他mysql索引。

不要忘记在doctrine/dbal文件中添加composer.json


0
投票

Migrations#Modifying Columns

看起来您所拥有的应该起作用:

Schema::table('SYS_tenants' ,function (Blueprint $table){
    $table->integer('tena_type')->unsigned()->nullable()->change();
});

取决于您的数据库,您可能需要将值转换为新类型:(对于mysql:https://www.mysqltutorial.org/mysql-cast/

© www.soinside.com 2019 - 2024. All rights reserved.