In fact there are so many similar questions, I could not solve this problem.
I am using codeigniter framework. When I call insert method of ActiveRecord by passing a php object, it send all properties either by its value or as null. It causes cannot be null. error.
Table Structure:
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;
Query I tried:
INSERT INTO `scheduledTasks` (`id`, `type`, `createTime`, `executionTime`, `ownerUserId`, `detail`, `isExecuted`)
VALUES (NULL, 'QuoteRequest', NULL, NULL, '926', NULL, NULL)
Mysql Version:
+-------------------------+
| VERSION() |
+-------------------------+
| 5.7.17-0ubuntu0.16.04.1 |
+-------------------------+
I have
explicit_defaults_for_timestamp | OFF
and sql mode is
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@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 |
+-------------------------------------------------------------------------------------------------------------------------------------------+
NULLcan trigger theDEFAULT_TIMESTAMP, thenNOT NULLwould be your problem. Otherwise, try inserting nothing forcreateTimeandexecutionTimeand see if that will triggerDEFAULT_TIMESTAMP. The manual is tricky. dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.htmlNULLto set aTIMESTAMP, but it does not say the same about settingDATETIME.