'Column of BIT datatype is not inserting/updating default values in MySql database using Spring Boot
So I have a table which is configured something like this
CREATE TABLE `jobDetails` (
`id` varchar(255) NOT NULL,
`job_id` varchar(255) NOT NULL,
`editor_id` varchar(255) DEFAULT NULL,
`editor_dueDate` datetime DEFAULT NULL,
`ops_tags` int NOT NULL DEFAULT '2',
`notes_from_ops` text CHARACTER SET utf8 COLLATE utf8_general_ci,
`reviewer_id` varchar(255) DEFAULT NULL,
`reviewer_dueDate` datetime DEFAULT NULL,
`sales_tags` varchar(255) DEFAULT '13',
`notes_from_sales` text CHARACTER SET utf8 COLLATE utf8_general_ci,
`shift` int DEFAULT NULL,
`additionalNotes` text CHARACTER SET utf8 COLLATE utf8_general_ci,
`editorStatus` int DEFAULT NULL,
`tracked_file_id` varchar(255) DEFAULT NULL,
`clean_file_id` varchar(255) DEFAULT NULL,
`additional_file_id` varchar(255) DEFAULT NULL,
`editor_wordCount` int DEFAULT NULL,
`job_details_type` int DEFAULT '0',
`editing_speed` int DEFAULT '0',
`reviewing_priority` int DEFAULT NULL,
`is_enabled` bit(1) NOT NULL DEFAULT b'1',
PRIMARY KEY (`id`),
KEY `FKhb282r593ce4xojoo4hfd3fwm` (`additional_file_id`),
KEY `FKnb8b8nhc9yb78e2p5ipadfc8u` (`clean_file_id`),
KEY `FK82594hccbyjik7bc0ev4f74ks` (`tracked_file_id`),
CONSTRAINT `FK82594hccbyjik7bc0ev4f74ks` FOREIGN KEY (`tracked_file_id`) REFERENCES `files` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FKhb282r593ce4xojoo4hfd3fwm` FOREIGN KEY (`additional_file_id`) REFERENCES `files` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
CONSTRAINT `FKnb8b8nhc9yb78e2p5ipadfc8u` FOREIGN KEY (`clean_file_id`) REFERENCES `files` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=latin1
So when I insert/update into this table through my code without setting its value then it should consider a default value to "true" or "1", but currently this is not the case it sets the value to "0" or "false".
My backend is using Spring Boot and Spring Data JPA
Solution 1:[1]
Without seeing your code might be difficult to answer, but here's a theory: you are (auto)initializing primitives (int or boolean) with their default value (0 or false), which is not null, so the DEFAULT keyword is ignored. You should also define default column values in Java, as shown here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | dcolazin |
