MySQL不接受ISO字符串作为DATE

时间:2019-02-11 23:36:13

标签: mysql

MySQL数据库将不接受ISO字符串,并引发此异常:

  

第1行“ dueDate”列的错误日期值:“ 2019-02-20T10:00:00.000Z”。

日期的格式如下:this.state.dueDate.toISOString(),作为ISO字符串。另外,在本地MySQL服务器上,不会引发任何错误。它只会使我的私有Ubuntu服务器失败。

这是表模式:

| tickets | CREATE TABLE `tickets` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `component` int(10) DEFAULT NULL,
  `name` varchar(200) CHARACTER SET utf32 COLLATE utf32_romanian_ci DEFAULT NULL,
  `description` varchar(2000) CHARACTER SET utf8 COLLATE utf8_romanian_ci DEFAULT NULL,
  `startDate` date DEFAULT NULL,
  `dueDate` date DEFAULT NULL,
  `category` int(10) DEFAULT NULL,
  `reporter` varchar(200) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
  `assignee` varchar(200) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
  `estimation` float DEFAULT '0',
  `priority` varchar(10) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
  `lane` varchar(15) CHARACTER SET ascii COLLATE ascii_bin DEFAULT NULL,
  `project` int(10) DEFAULT NULL,
  `releaseID` int(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `id` (`id`),
  KEY `releaseID` (`releaseID`),
  KEY `assignee` (`assignee`),
  KEY `reporter` (`reporter`),
  KEY `category` (`category`),
  KEY `component` (`component`) USING BTREE,
  CONSTRAINT `ticket_assignee_null` FOREIGN KEY (`assignee`) REFERENCES `users` (`email`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `ticket_comp_delete` FOREIGN KEY (`component`) REFERENCES `components` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `ticket_release_null` FOREIGN KEY (`releaseID`) REFERENCES `releases` (`id`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `ticket_reporter_null` FOREIGN KEY (`reporter`) REFERENCES `users` (`email`) ON DELETE SET NULL ON UPDATE CASCADE,
  CONSTRAINT `tikcet_category_null` FOREIGN KEY (`category`) REFERENCES `categories` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1 |

尝试更改默认字符集会导致以下错误: Cannot change column 'assignee': used in a foreign key constraint 'ticket_assignee_null'

2 个答案:

答案 0 :(得分:4)

MySQL手册涵盖了它在https://dev.mysql.com/doc/refman/8.0/en/date-and-time-literals.html中识别的格式

mysql> select DATE( '2019-02-20T10:00:00.000Z');
+-----------------------------------+
| DATE( '2019-02-20T10:00:00.000Z') |
+-----------------------------------+
| 2019-02-20                        |
+-----------------------------------+
1 row in set, 1 warning (0.00 sec)

Warning (Code 1292): Truncated incorrect datetime value: '2019-02-20T10:00:00.000Z'

您可以在MySQL中使用日期文字之前先更改格式,也可以在ISO日期文字上使用STR_TO_DATE()

mysql> select STR_TO_DATE( '2019-02-20T10:00:00.000Z', '%Y-%m-%dT%T.%fZ');
+-------------------------------------------------------------+
| STR_TO_DATE( '2019-02-20T10:00:00.000Z', '%Y-%m-%dT%T.%fZ') |
+-------------------------------------------------------------+
| 2019-02-20 10:00:00.000000                                  |
+-------------------------------------------------------------+

答案 1 :(得分:0)

问题与您的字符集和排序规则设置有关。将您本地数据库的字符集和排序规则设置与Ubuntu中的进行比较。您会看到问题。

SHOW VARIABLES LIKE  'char%';
SHOW VARIABLES LIKE  '%colla%';

请注意,排序规则和字符集设置可能在模式,表甚至列级别上有所不同。