How to properly set timestamp field seeder for a postgreSql database in Laravel 10.47?

38 Views Asked by At

I am using Laravel 10.47 and PostgreSql 14.2.

I an using the following piece of code in the migrations that creates the timestamps fields:

$table->timestamp('email_verified_at')->nullable()->useCurrentOnUpdate();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
$table->softDeletes()->nullable()->useCurrentOnUpdate();

Inside the seed code I have the following scenarios:

  1. I am not seeding created_at, email_verified_at, updated_at or deleted_at. In this case only created_at is seeded: enter image description here
  2. When I seed email_verified_at, updated_at or deleted_at with Carbon::now()->timestamp
class UserTableSeeder extends Seeder
{
    public function run(): void
    {
        DB::table('users')->insert([
        [
             'name' => 'Prof1',
             'middle_name' => 'Prof1',
             'last_name' => 'Prof1',
             'avatar' => 'prof1-Picture-S3',
             'role' => '0',
             'email' => '[email protected]',
             'password' => Hash::make('Prof1'),
             'remember_token' => 'some remember_token STRING',

             'email_verified_at' => Carbon::now()->timestamp,
             'updated_at' => Carbon::now()->timestamp,
             'deleted_at' => Carbon::now()->timestamp,
        ],
    }
}

I get the following error:

Database\Seeders\UserTableSeeder ......................................................................................................... RUNNING  

   Illuminate\Database\QueryException 

  SQLSTATE[22008]: Datetime field overflow: 7 ERROR:  date/time field value out of range: "1711657211"
HINT:  Perhaps you need a different "datestyle" setting.
CONTEXT:  unnamed portal parameter $2 = '...' (Connection: pgsql, SQL: insert into "users" ("avatar", "deleted_at", "email", "email_verified_at", "last_name", "middle_name", "name", "password", "remember_token", "role", "updated_at") values (prof1-Picture-S3, 1711657211, [email protected], 1711657211, Prof1, Prof1, Prof1, $2y$12$n9zt3JXLCCqP.N0LY2SVIeq1n.w3ifZQpTa2pWxo0lZKh7vzSAsl2, some remember_token STRING, 0, 1711657211), (prof2-Picture-S3, 1711657211, [email protected], 1711657211, Prof2, Prof2, Prof2, $2y$12$aPCTu8oup/PraiTE5cvQwOfUCOoThYhDF7gdyxTvuKGov0gFFPKD6, some remember_token STRING, 1, 1711657211), (Paci1-Picture-S3, 1711657212, [email protected], 1711657212, Paci1, Paci1, Paci1, $2y$12$pouVr40UAxCjtfQFd.srkeBo83Zm.ewYcE2iQeC9u7yue/OvoNte2, some remember_token STRING, 2, 1711657212))

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:829
    825▕                     $this->getName(), $query, $this->prepareBindings($bindings), $e
    826▕                 );
    827▕             }
    828▕ 
  ➜ 829▕             throw new QueryException(
    830▕                 $this->getName(), $query, $this->prepareBindings($bindings), $e
    831▕             );
    832▕         }
    833▕     }

      +7 vendor frames 

  8   database/seeders/UserTableSeeder.php:27
      Illuminate\Database\Query\Builder::insert()
      +8 vendor frames 

  17  database/seeders/DatabaseSeeder.php:16
      Illuminate\Database\Seeder::call("Database\Seeders\UserTableSeeder")

I don't know if the problem is because I am usingCarbon to seed a PostgreSql database. Is there a way to seed that without error?

UPDATE


If I use useCurrent() inside the Migration Class instead of useCurrentOnUpdate() it works.

Scenario that works - using useCurrent():

Inside the Migrations
$table->timestamp('email_verified_at')->nullable()->useCurrent();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrent();
$table->softDeletes()->nullable()->useCurrent();

Inside seeder
Not calling or not seeding `email_verified_at`, `created_at`, `updated_at` and `deleted_at`.

Scenario that do not works - using useCurrentOnUpdate():

Inside the Migrations
$table->timestamp('email_verified_at')->nullable()->useCurrentOnUpdate();
$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->nullable()->useCurrentOnUpdate();
$table->softDeletes()->nullable()->useCurrentOnUpdate();

Inside seeder

'email_verified_at' => Carbon::now()->timestamp,
'updated_at' => Carbon::now()->timestamp,
'deleted_at' => Carbon::now()->timestamp

I get the error SQLSTATE[22008]: Datetime field overflow:

0

There are 0 best solutions below