使用Laravel

时间:2018-05-03 16:03:51

标签: mysql laravel laravel-seeding

我正在使用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, uschimmel@example.net, 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, uschimmel@example.net, 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}
"}

1 个答案:

答案 0 :(得分:1)

似乎是一个奇怪的问题。

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