事实上有很多类似的问题,我无法解决这个问题。我正在使用CodeIgniter框架。当我通过传递PHP对象调用ActiveRecord的
表结构:
insert
方法时,它会将所有属性作为其值或null发送。这会导致不能为null
错误。表结构:
CREATE TABLE `scheduledTasks` (
`id` int(11) NOT NULL,
`type` varchar(255) COLLATE utf8_bin NOT NULL,
`createTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`executionTime` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
`ownerUserId` int(11) DEFAULT NULL,
`subjectUserId` text COLLATE utf8_bin,
`detail` text COLLATE utf8_bin,
`isExecuted` int(1) DEFAULT '0'
);
ALTER TABLE `scheduledTasks`
ADD PRIMARY KEY (`id`);
ALTER TABLE `scheduledTasks`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;
我尝试的查询:
INSERT INTO `scheduledTasks` (`id`, `type`, `createTime`, `executionTime`, `ownerUserId`, `detail`, `isExecuted`)
VALUES (NULL, 'QuoteRequest', NULL, NULL, '926', NULL, NULL)
MySQL版本:
+-------------------------+
| VERSION() |
+-------------------------+
| 5.7.17-0ubuntu0.16.04.1 |
+-------------------------+
我
explicit_defaults_for_timestamp | OFF
并且 SQL 模式是
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@SESSION.sql_mode |
+-------------------------------------------------------------------------------------------------------------------------------------------+
| ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+-------------------------------------------------------------------------------------------------------------------------------------------+
NULL
可以触发DEFAULT_TIMESTAMP
,那么NOT NULL
就会成为你的问题。否则,尝试不插入任何内容到createTime
和executionTime
,看看是否会触发DEFAULT_TIMESTAMP
。这个手册有些棘手。https://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html - Anthony RutledgeNULL
来设置TIMESTAMP
,但它没有提到设置DATETIME
的情况。 - Anthony Rutledge