Django migration sql for conditional triggers

前端 未结 2 1019
离开以前
离开以前 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.)

提交回复
热议问题