Mysql 无效的日期时间格式 : 1292 Incorrect datetime value when creating some dummy data for testing

标签 mysql datetime

在向表中插入一些行时出现此错误:

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: '2017-03-26 02:00:00' for column 'created _at' at row 1

我以正确的格式插入:Y-m-d H:i:s 那么是什么导致了问题?

更新:

CREATE TABLE `posts` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `title` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `excerpt` text COLLATE utf8mb4_unicode_ci,
  `body` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `image_path` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `image` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `slug` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `markdown` tinyint(1) NOT NULL DEFAULT '0',
  `published` tinyint(1) NOT NULL DEFAULT '0',
  `views` int(10) UNSIGNED NOT NULL DEFAULT '0',
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `published_at` datetime DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

我正在使用 500 次重复的循环将虚拟数据插入此表,它设法插入 472 个帖子,然后因上述错误而失败。

下面是生成的 sql 查询的样子,我不是自己写的,但是当我使用 php 对象和方法创建等时,我使用的框架会生成它

    "query" => "insert into `posts` (`user_id`, `title`, `excerpt`, `body`, `s
lug`, `image`, `created_at`, `updated_at`, `published_at`, `published`, `views`) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)3
8;5;208m"
    "bindings" => array:11 [
      0 => 1
      1 => "Enim autem et quis consequatur vel deleniti et."
      2 => "Voluptatibus nobis et et quaerat at inventore incidunt et qui ea co"
      3 => """
        Soluta distinctio dolorem ut officiis dolores quisquam esse sint. Repellendus est atque enim
 quia.\n
        """
      4 => "ab-itaque-facilis-culpa-suscipit-et-ratione-doloremque"

      5 => "post_img_1.jpg"
      6 => "2017-03-06 10:00:00"
      7 => "2017-03-06 10:00:00"
      8 => "2017-03-06 17:00:00"
      9 => true
      10 => 49
    ]
    "time" => 29.97

最佳答案

您的数据存储在哪个时区?

我认为问题不在于日期时间值的格式,而在于值本身。

我注意到您的一些数据是意大利语的,导致错误的值是 2017-03-26 02:00:00,这在意大利实际上是一个无效的日期时间,因为那是白天开始节省时间。

意大利的时钟将从 2017-03-26 01:59:59 跳到 2017-03-26 03:00:00,因此您的值正在尝试插入无效。

关于Mysql 无效的日期时间格式 : 1292 Incorrect datetime value when creating some dummy data for testing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42809973/

相关文章:

php彩票问题多人中奖问题

c# - MySql.数据.MySqlClient.MySqlException : Timeout expired

javascript - knex.js 迁移中的时间戳字段

python - 为什么 PST(美国/洛杉矶)时间转换为 UTC 时间晚 1 小时?

sql - 如何避免此查询中出现相同的输出时间?

mysql - 跨多行匹配,无需太多连接

mysql - 选择查询返回错误结果

php - SQL JOIN——两个团队,一个标识数据库

python - 如何使用日期时间检索行(python 和 mysql)

python - pd.to_datetime() 解析正确的日期格式时出现奇怪的错误