我无法使用生成的存储主键在表中插入数据。
我的桌子是:
CREATE TABLE `living_complex` (
`id` bigint(32) GENERATED ALWAYS AS (`location_id`) STORED NOT NULL,
`location_id` int(11) NOT NULL,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unq_living_complex_name` (`name`),
KEY `idx_living_complex_location_id` (`location_id`),
KEY `idx_living_complex_name` (`name`),
CONSTRAINT `fk_living_complex_location_id` FOREIGN KEY (`location_id`) REFERENCES `location` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
我的插入语句:
INSERT INTO `reeve`.`living_complex` (`location_id`, `name`)
VALUES ('1', 'some_living_complex');
错误是:
1048:列“ id”不能为空
我在create语句中没有NOT NULL关键字。为什么要求输入ID?
答案 0 :(得分:0)
知道了。触发器出错
CREATE DEFINER=`root`@`localhost` TRIGGER `reeve`.`trg_living_complex_object_id` BEFORE INSERT ON `living_complex` FOR EACH ROW
BEGIN
INSERT INTO `reeve`.`object` (
`id`,
`object_type_id`
)
VALUES (
new.id,
(SELECT `object_type`.`id` FROM `reeve`.`object_type` WHERE `table_name` = 'living_complex')
);
END
应该在插入之后。因为在插入之前我没有NEW.id(由表达式生成)。