1

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

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');
       });
}

4

4 回答 4

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 -> 数据库:迁移 -> 修改列

于 2020-02-10T06:38:15.607 回答
0

我已经使用了这个 Laravel 迁移

$table->integer('tena_type')->unsigned()->nullable()->change();

但它不起作用,因为数据已经在表中。在那种情况下,它不能更改数据类型。我使用这个 DB 语句来更改数据类型。它工作正常。

DB::statement("alter table SYS_tenants modify tena_type integer not null"):

于 2020-02-12T06:22:26.377 回答
0

迁移#修改列

看起来你应该工作:

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

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

于 2020-02-10T06:31:10.227 回答
0

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

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

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

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

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

于 2020-02-10T06:34:26.173 回答