I'm trying to migrate a migration file. Some integer fields triggered an error:
$table->integer('age',11)->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();
This time, no error raised, but the created table miss and those fields!
Please find the whole file below:
<?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');
}
}
CodePudding user response:
You can't set length, but you can use different types of integer:
$table->bigInteger()
$table->mediumInteger()
$table->integer()
$table->smallInteger()
$table->tinyInteger()
You can check documentation as well.
CodePudding user response:
I believe you want to set the length of the age
column integer type.
Unfortunately, the second parameter is not for setting the column length. You can set the column length for char
and string
types only, but not for integer
.
Illuminate\Database\Schema\Blueprint
:
integer($column, $autoIncrement = false, $unsigned = false)
Automatically, migration will create the integer
type with length 11
. So you don't have to bother.
$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();
The change
method is only used to update the column, not to create the column. You won't see the error, because migration ignore it.
To update the table, you must have a schema like this:
Schema::table('reservation_anniversaires', function (Blueprint $table) {
...
});
// Schema::create for creating
CodePudding user response:
The second parameter of all the integer fields is autoincrement
, not size
. Setting this to anything that's truthy will result in the field being auto incremented. This is probably not what you're attempting here.
I suggest you use any of the available methods for the size:
tinyInteger
, smallInteger
, mediumInteger
, integer
, bigInteger
,
unsignedTinyInteger
, unsignedSmallInteger
, unsignedMediumInteger
, unsignedInteger
, unsignedBigInteger
Or any of the auto incrementing helpers:
tinyIncrements
, smallIncrements
, mediumIncrements
, increments
, bigIncrements
Also, you don't need change()
unless you're actually changing the table structure in a follow-up migration.
Source: https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Schema/Blueprint.php#L756-L759
Available column types: https://laravel.com/docs/8.x/migrations#available-column-types
Modifying columns: https://laravel.com/docs/8.x/migrations#updating-column-attributes