I’m running 0.117.6 and noticed this morning I had no history data any more. I checked the logs, and it’s full of messages like:
MySQLdb._exceptions.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`hass`.`states`, CONSTRAINT `states_ibfk_2` FOREIGN KEY (`old_state_id`) REFERENCES `states` (`state_id`))')
I tried executing the commands, and the first set worked, but not the second:
ALTER TABLE states DROP FOREIGN KEY states_ibfk_1;
ALTER TABLE states ADD CONSTRAINT states_ibfk_1 FOREIGN KEY (event_id) REFERENCES events (event_id) ON DELETE SET NULL;
ALTER TABLE states DROP FOREIGN KEY states_ibfk_2;
ALTER TABLE states ADD CONSTRAINT states_ibfk_2 FOREIGN KEY (old_state_id) REFERENCES states (state_id) ON DELETE SET NULL;
I get:
Cannot add or update a child row: a foreign key constraint fails (`hass`.`#sql-241_2dd78`, CONSTRAINT `states_ibfk_2` FOREIGN KEY (`old_state_id`) REFERENCES `states` (`state_id`) ON DELETE SET NULL)
For ref, when I do a SHOW CREATE TABLE states
, I get:
CREATE TABLE `states` (
`state_id` int(11) NOT NULL AUTO_INCREMENT,
`domain` varchar(64) DEFAULT NULL,
`entity_id` varchar(255) DEFAULT NULL,
`state` varchar(255) DEFAULT NULL,
`attributes` text DEFAULT NULL,
`event_id` int(11) DEFAULT NULL,
`last_changed` datetime DEFAULT NULL,
`last_updated` datetime DEFAULT NULL,
`created` datetime DEFAULT NULL,
`old_state_id` int(11) DEFAULT NULL,
PRIMARY KEY (`state_id`),
KEY `old_state_id` (`old_state_id`),
KEY `ix_states_entity_id_last_updated` (`entity_id`,`last_updated`),
KEY `ix_states_event_id` (`event_id`),
KEY `ix_states_last_updated` (`last_updated`),
CONSTRAINT `states_ibfk_1` FOREIGN KEY (`event_id`) REFERENCES `events` (`event_id`) ON DELETE SET NULL
) ENGINE=InnoDB AUTO_INCREMENT=324643 DEFAULT CHARSET=utf8