Django migration sql for conditional triggers

前端 未结 2 1020
离开以前
离开以前 2021-01-29 04:53

I want to create a trigger that only inserts into table when the condition is met. I\'ve tried using various combinations of IF/BEGIN/END and WHERE, but Django returns me an SQ

相关标签:
2条回答
  • 2021-01-29 05:10

    @variables are not DECLARED.

    Either:

    DECLARE user_same BOOLEAN;
    SELECT 1 INTO user_same WHERE ...
    

    or

    SELECT @user_same := 1 WHERE ...
    

    Better yet, this avoids the need for the variable.

    IF (EXISTS SELECT * FROM ...)
    

    Also, don't use the construct IN ( SELECT ... ); it is usually better to use WHERE EXISTS ( SELECT * FROM ... ) or JOIN ... ON ....

    (There may be more problems after fixing those.)

    0 讨论(0)
  • 2021-01-29 05:13

    Another easy way to do this.

    You should add any symbols between ; delimiters for the appropriate converting MySQL syntax to the raw SQL.

    CREATE TRIGGER trigger_name BEFORE INSERT ON table
    FOR EACH ROW
    BEGIN
    IF NEW.number <> 'anynumber' AND NEW.number <> 'anynumber'
      THEN
        SET NEW.number = 'anynumber'; --
    END IF; --
    END
    

    This example works due to the dash symbols.

    0 讨论(0)
提交回复
热议问题