dongming4994 2019-04-07 03:05
浏览 95

Laravel:播种MySQL表时的日期时间格式无效

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

I have a table named 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 problems. 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 a few hours ago, reading data, writing data, etc., it just stopped working. Of course, I made no changes to the database configuration or any other thing rather than adding the role column.

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)