我使用的是Laravel 5.6,我使用的是一个数据库播种器,在开发时播种我的本地mysql数据库。以下是播种机的一些代码:

class DatabaseSeeder extends Seeder
{
    $products = factory(App\Product::class, 100)->create();

    foreach ($products as $product) {
        // Assign some reviews to each product
        $reviews = factory(App\ProductReview::class, rand(0, 10))->make();
        $product->reviews()->saveMany($reviews);
    }
}

播种器大部分时间都按预期工作,但在播种时,我将随机收到一个错误,关于某个列的无效日期时间格式。我不知道是什么导致了这种情况,特别是因为这个问题只出现在播种机运行的20%左右。另外,基于这个错误,我看不出日期时间格式有什么问题,所以我不确定发生了什么。
它似乎也不仅仅限于评论,其他时候我会看到错误,而播种机正在试图创建一个产品。我将在下面粘贴完整的错误跟踪。希望有人能给我指一个正确的方向,告诉我这是什么造成的。
[2018-05-03 15:51:14] local.ERROR: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2008-03-09 02:18:48' for column 'created_at' at row 1 (SQL: insert into `product_reviews` (`product_id`, `order_id`, `name`, `email`, `location`, `rating`, `text`, `title`, `status`, `is_manually_verified`, `manual_verification_note`, `created_at`, `updated_at`) values (41, , Jaleel Corkery, [email protected], Maeganhaven, Vermont, 1, Incidunt earum inventore facere eum. Repudiandae ipsam similique laboriosam et illum autem. Exercitationem rerum expedita est iste. Enim necessitatibus eaque delectus incidunt ullam inventore., Sed est qui vero., approved, 0, , 2008-03-09 02:18:48, 2018-05-03 15:51:14)) {"exception":"[object] (Illuminate\\Database\\QueryException(code: 22007): SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2008-03-09 02:18:48' for column 'created_at' at row 1 (SQL: insert into `product_reviews` (`product_id`, `order_id`, `name`, `email`, `location`, `rating`, `text`, `title`, `status`, `is_manually_verified`, `manual_verification_note`, `created_at`, `updated_at`) values (41, , Jaleel Corkery, [email protected], Maeganhaven, Vermont, 1, Incidunt earum inventore facere eum. Repudiandae ipsam similique laboriosam et illum autem. Exercitationem rerum expedita est iste. Enim necessitatibus eaque delectus incidunt ullam inventore., Sed est qui vero., approved, 0, , 2008-03-09 02:18:48, 2018-05-03 15:51:14)) at /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, PDOException(code: 22007): SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2008-03-09 02:18:48' for column 'created_at' at row 1 at /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Connection.php:458)
[stacktrace]
#0 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Connection.php(624): Illuminate\\Database\\Connection->runQueryCallback('insert into `pr...', Array, Object(Closure))
#1 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Connection.php(459): Illuminate\\Database\\Connection->run('insert into `pr...', Array, Object(Closure))
#2 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Connection.php(411): Illuminate\\Database\\Connection->statement('insert into `pr...', Array)
#3 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php(32): Illuminate\\Database\\Connection->insert('insert into `pr...', Array)
#4 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(2349): Illuminate\\Database\\Query\\Processors\\Processor->processInsertGetId(Object(Illuminate\\Database\\Query\\Builder), 'insert into `pr...', Array, 'id')
#5 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1285): Illuminate\\Database\\Query\\Builder->insertGetId(Array, 'id')
#6 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(726): Illuminate\\Database\\Eloquent\\Builder->__call('insertGetId', Array)
#7 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(691): Illuminate\\Database\\Eloquent\\Model->insertAndSetId(Object(Illuminate\\Database\\Eloquent\\Builder), Array)
#8 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(554): Illuminate\\Database\\Eloquent\\Model->performInsert(Object(Illuminate\\Database\\Eloquent\\Builder))
#9 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(250): Illuminate\\Database\\Eloquent\\Model->save()
#10 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/HasOneOrMany.php(262): Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany->save(Object(App\\ProductReview))
#11 /Users/me/dev/corp/platform/database/seeds/DatabaseSeeder.php(53): Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany->saveMany(Object(Illuminate\\Database\\Eloquent\\Collection))
#12 [internal function]: DatabaseSeeder->run()
#13 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#14 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#15 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#16 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#17 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Seeder.php(122): Illuminate\\Container\\Container->call(Array)
#18 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(63): Illuminate\\Database\\Seeder->__invoke()
#19 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/GuardsAttributes.php(122): Illuminate\\Database\\Console\\Seeds\\SeedCommand->Illuminate\\Database\\Console\\Seeds\\{closure}()
#20 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Console/Seeds/SeedCommand.php(64): Illuminate\\Database\\Eloquent\\Model::unguarded(Object(Closure))
#21 [internal function]: Illuminate\\Database\\Console\\Seeds\\SeedCommand->handle()
#22 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#23 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#24 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#25 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#26 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\\Container\\Container->call(Array)
#27 /Users/me/dev/corp/platform/vendor/symfony/console/Command/Command.php(252): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArrayInput), Object(Illuminate\\Console\\OutputStyle))
#28 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArrayInput), Object(Illuminate\\Console\\OutputStyle))
#29 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Console/Command.php(198): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArrayInput), Object(Illuminate\\Console\\OutputStyle))
#30 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/RefreshCommand.php(129): Illuminate\\Console\\Command->call('db:seed', Array)
#31 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Database/Console/Migrations/RefreshCommand.php(68): Illuminate\\Database\\Console\\Migrations\\RefreshCommand->runSeeder(NULL)
#32 [internal function]: Illuminate\\Database\\Console\\Migrations\\RefreshCommand->handle()
#33 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(29): call_user_func_array(Array, Array)
#34 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(87): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#35 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(31): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Illuminate\\Foundation\\Application), Array, Object(Closure))
#36 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Container/Container.php(564): Illuminate\\Container\\BoundMethod::call(Object(Illuminate\\Foundation\\Application), Array, Array, NULL)
#37 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Console/Command.php(183): Illuminate\\Container\\Container->call(Array)
#38 /Users/me/dev/corp/platform/vendor/symfony/console/Command/Command.php(252): Illuminate\\Console\\Command->execute(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#39 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Console/Command.php(170): Symfony\\Component\\Console\\Command\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Illuminate\\Console\\OutputStyle))
#40 /Users/me/dev/corp/platform/vendor/symfony/console/Application.php(865): Illuminate\\Console\\Command->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#41 /Users/me/dev/corp/platform/vendor/symfony/console/Application.php(241): Symfony\\Component\\Console\\Application->doRunCommand(Object(Illuminate\\Database\\Console\\Migrations\\RefreshCommand), Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#42 /Users/me/dev/corp/platform/vendor/symfony/console/Application.php(143): Symfony\\Component\\Console\\Application->doRun(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#43 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Console/Application.php(88): Symfony\\Component\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#44 /Users/me/dev/corp/platform/vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php(122): Illuminate\\Console\\Application->run(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#45 /Users/me/dev/corp/platform/artisan(37): Illuminate\\Foundation\\Console\\Kernel->handle(Object(Symfony\\Component\\Console\\Input\\ArgvInput), Object(Symfony\\Component\\Console\\Output\\ConsoleOutput))
#46 {main}
"}

最佳答案

似乎是个奇怪的问题。
2008-03-09 02:18:48
“日期/时间看起来是有效的,但实际上是在周日凌晨2点到3点之间,时间从2点提前到3点。所以,2到凌晨3点之间的任何时间都不存在。
将日期/时间转换为正确的时区可以解决此问题。
文件中:config/app.php
'timezone' => 'UTC', //change UTC to your time zone, eg: 'America/Havana'
参考:https://github.com/fzaninotto/Faker/issues/1078

关于mysql - 使用Laravel播种Mysql数据库时出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50159435/

10-11 00:18