Laravel 迁移 运行 没有错误,但未创建整数字段

Laravel migrations run with no error but integer field is not created

我正在尝试迁移迁移文件。某些整数字段触发了错误:

$table->integer('age',11)->default('0');

其中有一些。在阅读了其他帖子并尝试了一些操作后,我将其更改为:

$table->integer('age',11)->default(0)->change();

这一次,没有出现错误,但是创建了 table 未命中和那些字段!

请在下面找到整个文件:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateReservationAnniversairesTable extends Migration
{
    public function up()
    {
        Schema::create('reservation_anniversaires', function (Blueprint $table) {

        $table->increments('id',11);
        $table->string('nom',50)->nullable()->default('NULL');
        $table->string('gsm',30)->nullable()->default('NULL');
        $table->string('prenom',50)->nullable()->default('NULL');
        $table->string('email',100);
        $table->integer('age',11)->default(0)->change();
        $table->time('heure_debut');
        $table->time('heure_fin');
        $table->string('gateau',100)->nullable()->default('NULL');
        $table->string('impression',100)->nullable()->default('NULL');
        $table->integer('nbre_enfants',11)->default(0)->change();
        $table->integer('acompte',11)->default(0)->change();
        $table->text('remarques');
        $table->tinyInteger('salle',4)->default(0)->change();
        $table->time('heure_gouter');
        $table->date('date');
        $table->string('formule',50)->nullable()->default('NULL');
        $table->integer('actif',11)->default(1)->change();
        $table->string('couleur',30)->nullable()->default('NULL');
        $table->integer('rappel',11)->default(0)->change();
        $table->integer('confirm',11)->default(0)->change();
        $table->integer('pizzas',11)->default(0)->change();
        $table->integer('sandwiches',11)->default(0)->change();
        $table->string('options')->nullable()->default('NULL');
        $table->integer('annif_confirme',11)->default(0)->change();
        $table->string('created',30);
        $table->string('modified',30);
        $table->tinyInteger('confirm_enfants',1)->default(0)->change();
        $table->tinyInteger('invitations',1)->default(0)->change();
        $table->string('adresse')->nullable()->default('NULL');
        $table->integer('invitsEnvoyees',11)->default(0)->change();
        $table->float('paiement')->default(0)->change();
        $table->string('paiement_methode',50)->nullable()->default('NULL');
        $table->integer('enfants_presents',11)->default(0)->change();
        $table->integer('parts_gateau',11)->default(0)->change();
        $table->float('options_montant')->default(0)->change();
        $table->string('deco_salle',50)->nullable()->default('NULL');
        $table->string('formule_theme',50)->nullable()->default('NULL');
        $table->string('photos',50)->nullable()->default('NULL');
        $table->string('grimage',50)->nullable()->default('NULL');
        $table->string('clown',50)->nullable()->default('NULL');
        $table->string('sculpture',50)->nullable()->default('NULL');

        });
    }

    public function down()
    {
        Schema::dropIfExists('reservation_anniversaires');
    }
}

不能设置长度,但可以使用不同类型的整数:

$table->bigInteger()
$table->mediumInteger()
$table->integer()
$table->smallInteger()
$table->tinyInteger()

您也可以查看 documentation

我相信你想设置 age 列整数类型的长度。

不幸的是,第二个参数不是用于设置列长度的。您只能为 charstring 类型设置列长度,但不能为 integer.

设置列长度

Illuminate\Database\Schema\Blueprint :

integer($column, $autoIncrement = false, $unsigned = false)

迁移将自动创建长度为 11integer 类型。所以你不必费心。

$table->integer('age')->default(0);

There were a few of those. After reading other posts, and trying a few things, I changed it to:

$table->integer('age',11)->default(0)->change();

change方法只用于更新列,不用于创建列。您不会看到该错误,因为迁移会忽略它。

要更新 table,您必须具有如下架构:

Schema::table('reservation_anniversaires', function (Blueprint $table) {
    ...
});

// Schema::create for creating

所有整数字段的第二个参数是autoincrement,而不是size。将此设置为任何真实的值都会导致该字段自动递增。这可能不是您在这里尝试的。

我建议您使用任何可用的尺寸方法:
tinyInteger, smallInteger, mediumInteger, integer, bigInteger, unsignedTinyIntegerunsignedSmallIntegerunsignedMediumIntegerunsignedIntegerunsignedBigInteger

或任何自动递增助手: tinyIncrementssmallIncrementsmediumIncrementsincrementsbigIncrements

此外,您不需要 change(),除非您实际上是在 follow-up 迁移中更改 table 结构。

来源:https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Schema/Blueprint.php#L756-L759
可用的列类型:https://laravel.com/docs/8.x/migrations#available-column-types
修改列:https://laravel.com/docs/8.x/migrations#updating-column-attributes