简体   繁体   中英

MYSQL ERROR 1422: Explicit or implicit commit is not allowed in stored function or trigger

I am trying to make a before_insert trigger in case two columns are null to not allow data insertion. The idea I tried to apply was to create the notnull constraint on both columns at the time of insertion and later remove that constraint.

I don't know much about SQL and I tried to use the following code:

CREATE DEFINER = CURRENT_USER TRIGGER `db_autoescola`.`contas_recebers_BEFORE_INSERT` BEFORE INSERT ON `contas_recebers` FOR EACH ROW
BEGIN
    IF NEW.id_recepcionista IS NULL and NEW.id_instrutor IS NULL
    THEN ALTER TABLE contas_recebers MODIFY id_recepcionista INT NOT NULL;
         ALTER TABLE contas_recebers MODIFY id_instrutor INT NOT NULL;
    END IF;
END

However, when trying to apply this trigger I have the following error and I was unable to identify what is needed to correct it.

Operation failed: There was an error while applying the SQL script to the database.
Executing:
DROP TRIGGER IF EXISTS `db_autoescola`.`contas_recebers_BEFORE_INSERT`;

DELIMITER $$
USE `db_autoescola`$$
CREATE DEFINER = CURRENT_USER TRIGGER `db_autoescola`.`contas_recebers_BEFORE_INSERT` BEFORE INSERT ON `contas_recebers` FOR EACH ROW
BEGIN
    IF NEW.id_recepcionista IS NULL and NEW.id_instrutor IS NULL
    THEN ALTER TABLE contas_recebers MODIFY id_recepcionista INT NOT NULL;
         ALTER TABLE contas_recebers MODIFY id_instrutor INT NOT NULL;
    END IF;
END$$
DELIMITER ;

ERROR 1422: Explicit or implicit commit is not allowed in stored function or trigger.
SQL Statement:
CREATE DEFINER = CURRENT_USER TRIGGER `db_autoescola`.`contas_recebers_BEFORE_INSERT` BEFORE INSERT ON `contas_recebers` FOR EACH ROW
BEGIN
    IF NEW.id_recepcionista IS NULL and NEW.id_instrutor IS NULL
    THEN ALTER TABLE contas_recebers MODIFY id_recepcionista INT NOT NULL;
         ALTER TABLE contas_recebers MODIFY id_instrutor INT NOT NULL;
    END IF;
END

The use of DDL (Data Definition Language) as

 ALTER TABLE 

produce an implicit commit and this is not allowed in a trigger. You can't change the data structure runtime, these operation must be performed before you manipulate the data.

Thanks @scaisEdge, with that in mind I was able to come up with a solution:

Instead of trying to change the table I created a notnull column to check when the two are null, that way I will pass the null value to it if true or any value otherwise. This way I can receive an exception in my application and treat it the way I want.

Code:

CREATE DEFINER = CURRENT_USER TRIGGER `contas_recebers_BEFORE_INSERT` BEFORE INSERT ON `contas_recebers` FOR EACH ROW BEGIN
    IF NEW.id_recepcionista IS NULL and NEW.id_instrutor IS NULL
    THEN SET NEW.ids_null = null;
    else SET NEW.ids_null = TRUE;
    END IF;
END

I have in mind that this may even be a silly problem and the solution is not one of the best, but it is the way I managed to solve it, maybe it will help someone else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM