[MDEV-26846] SQL Error: Field 'DB_ROW_HASH_1' doesn't have a default value Created: 2021-10-18  Updated: 2021-10-18  Resolved: 2021-10-18

Status: Closed
Project: MariaDB Server
Component/s: Data Manipulation - Insert, Triggers
Affects Version/s: 10.6.4, 10.4, 10.5, 10.6
Fix Version/s: N/A

Type: Bug Priority: Major
Reporter: Sergey Khatin Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: UNIQUE, default, hash, key, triggers, value
Environment:

Windows 10 Pro x64 20H2


Issue Links:
Duplicate
duplicates MDEV-22756 SQL Error (1364): Field 'DB_ROW_HASH_... Closed

 Description   

There are two tables and a trigger that generates the value of one of the columns of the second table before inserting a new row:

CREATE TABLE `table1` (
  `Id` char(36) NOT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `Id_UN` (`Id`)
);
 
CREATE TABLE `table2` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `Index` int(11) NOT NULL,
  `Value` varchar(1024) NOT NULL,
  `FirstTableId` char(36) NOT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `Id_UN` (`Id`),
  UNIQUE KEY `Value_UN` (`Value`,`FirstTableId`) USING HASH,
  KEY `IX_FK_Table1Table2` (`FirstTableId`),
  CONSTRAINT `FK_Table1Table2` FOREIGN KEY (`FirstTableId`) REFERENCES `table1` (`Id`) ON DELETE CASCADE ON UPDATE NO ACTION
);
 
DELIMITER ;;
 
CREATE TRIGGER `OnTable2BeforeInsert`
BEFORE INSERT
ON table2 FOR EACH ROW
BEGIN
    IF(SELECT COUNT(*) FROM `table2` WHERE `FirstTableId` = NEW.`FirstTableId`) = 0 THEN
        SET NEW.`Index` = 1;
    ELSE
        SET NEW.`Index` = (SELECT MAX(`Index`) FROM `table2` WHERE `FirstTableId` = NEW.`FirstTableId`)+1;
    END IF;
END
 
;;

An error occurs when inserting a row into the second table:

 
INSERT INTO table1 (Id) VALUES ('7a4b8cc2-913b-43a9-ac3c-d9afb46fde98');
INSERT INTO table2 (Value, FirstTableId)VALUES ('123', '7a4b8cc2-913b-43a9-ac3c-d9afb46fde98');

Error: SQL Error [1364] [HY000]: (conn=37) Field 'DB_ROW_HASH_1' doesn't have a default value



 Comments   
Comment by Alice Sherepa [ 2021-10-18 ]

Thanks!
This is the same problem as MDEV-22756

Generated at Thu Feb 08 09:48:24 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.