samedi 6 avril 2019

Laravel: invalid datetime format when seeding a MySQL table

This came out of the blue and I have no idea why is happenning or what it caused.

I have this table users...

Schema::create('users', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name');
  $table->string('nick')->unique();
  $table->string('password');
  $table->string('email')->unique();
  $table->string('avatar')->default('avatar/default.png');

  $table->string('role')->default('user');

  $table->string('status')->default('active');
  $table->text('suspension_reason')->nullable();
  $table->integer(' referred_by')->unsigned()->nullable();
  $table->timestamp('referred_date')->nullable();
  $table->text('referred_message')->nullable();
  $table->timestamp('email_verified_at')->nullable();
  $table->rememberToken();
  $table->timestamps();
}

Since I'm developing, I've been doing

php artisan migrate:fresh --seed

all day long without ptoblems. In the last iteration I added this column role that you see separated from the others, that shouldn't be a problem.

The seeder I'm using for this table is:

  $users = [
    ['name' => 'Alfa', 'nick' => 'alfa', 'password' => '123123', 'email' => 'alfa@gmail.com', 'status' => 'active', 'role'=>"deputy"],
    ['name' => 'Beta Beta', 'nick' => 'beta', 'password' => '123123', 'email' => 'beta@gmail.com', 'status' => 'invited', 'role'=>'user'],
    ['name' => 'Charlie Charlie', 'nick' => 'charlie', 'password' => '123123', 'email' => 'charlie@gmail.com', 'status' => 'suspended', 'suspension_reason' => 'Mala Conducta', 'role'=>'user'],
  ];

  foreach ($users as $u) {
    $user = User::create([
      'name'              => $u['name'],
      'nick'              => $u['nick'],
      'email'             => $u['email'],
      'password'          => bcrypt($u['password']),
      'status'            => $u['status'],
      'role'              => $u['role'],
      'suspension_reason' => $u['suspension_reason'] ?? NULL,
    ]);
    $user->save();
  }

As you see, nothing particular here. It was working all day long.

Now, after adding the column "role", I got the following error message from PDO:

Illuminate\Database\QueryException  : SQLSTATE[22007]: 
Invalid datetime format: 1292 Incorrect datetime value: 
'2019-04-07 02:52:04' for column 'updated_at' at row 1 
(SQL: insert into `users` (`name`, `nick`, `email`, `password`, 
`status`, `role`, `suspension_reason`, `updated_at`, `created_at`) 
values (Alfa, alfa, alfa@gmail.com, 
$2y$10$uy5PtZ8RPyGCOOH1DXO8NeNtN2cVAwgtTtPXvVFIzUYAKAi8RpCJO, 
active, deputy, , 2019-04-07 02:52:04, 2019-04-07 02:52:04))

I added line breaks for legibility, but this comes in one line.

I have read several answers to somewhat similar questions, and all refer to a problem with the datetime format, which I don't see it, since it is Y-m-d H:m:s as mySQL requires it. Anyway, this was working perfectly few hours ago, reading data, writing data, etc., it just stopped working. Of course, I made no changes to the database configuration, or anyother thing rather than adding the role column.

Of course, now, if I delete this change it still doesn't work.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire