【问题标题】:Django migration sql for conditional triggers用于条件触发器的 Django 迁移 sql
【发布时间】:2018-05-23 00:06:25
【问题描述】:

我想创建一个仅在满足条件时才插入到表中的触发器。 我尝试过使用 IF/BEGIN/END 和 WHERE 的各种组合,但 Django 每次都返回一个 SQL 语法错误。

这里type_user_id指的是触发点赞的人,user_id指的是收到通知的人。它应该只在他们不是同一个人时触发。

operations = [

    migrations.RunSQL(
        "DROP TRIGGER like_notification",
        """
        CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
        FOR EACH ROW
        IF (select user_id from channel WHERE channel_id IN
            (SELECT channel_id FROM article WHERE article_id = NEW.article_id)) <> NEW.user_id
        BEGIN
            INSERT INTO notification (type, type_id, article_id, is_read, type_user_id, user_id)
                VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id,
                    (SELECT user_id from channel WHERE channel_id IN
                        (SELECT channel_id FROM article WHERE article_id = NEW.article_id)
                    )
                )
        END;
        """
    )
]

>>>django.db.utils.ProgrammingError: (1064, "You have an error in your SQL syntax; 
check the manual that corresponds to your MariaDB server version for the right syntax 
to use near 'BEGIN\n INSERT INTO notification (type, type_id, article_id, is_re' at line 5")

和

CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
        FOR EACH ROW
        BEGIN
            DECLARE @user_same Boolean;
            SELECT 1 into @user_same
            FROM channel
            WHERE channel_id IN (select channel_id FROM article where article_id = NEW.article_id)
            AND user_id = NEW.user_id;

            IF @user_same <> 1
            THEN
                INSERT INTO notification (type, type_id, article_id, is_read, type_user_id, user_id,)
                VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id, (
                    SELECT user_id from channel WHERE channel_id IN (
                         SELECT channel_id FROM article WHERE article_id = NEW.article_id)
                    )
            END IF;
        END;

>>>You have an error in your SQL syntax; check the manual that corresponds to your 
MariaDB server version for the right syntax to use near '@user_same Boolean;\n                SELECT 1 into @user_same\n                FRO' at line 4"

和

CREATE TRIGGER like_notification AFTER INSERT ON user_likes_article
        FOR EACH ROW
        INSERT INTO notification (type, type_id, article_id, is_read, type_user_iduser_id)
        VALUES ('like', NEW.article_id, NEW.article_id, false, NEW.user_id,
            (
            SELECT user_id from channel WHERE channel_id IN
                (SELECT channel_id FROM article WHERE article_id = NEW.article_id)
            )
        WHERE NOT user_id = NEW.user_id;

>>>You have an error in your SQL syntax; check the manual that corresponds to your 
MariaDB server version for the right syntax to use near 'WHERE NOT user

我真的不明白为什么它们都不起作用。任何帮助将非常感激!

【问题讨论】:

  • 首先,如果你得到一个错误,你应该显示它。其次,你为什么要这样做?如果您正在编写 Django 应用程序,则应该使用 Django 功能,例如信号和模型层。
  • @DanielRoseman 我已经对其进行了编辑以包含错误!这是一个学校数据库项目,它需要原始 sql 和 django。

标签: mysql django mariadb


【解决方案1】:

@variables 不是DECLARED。

要么:

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

或

SELECT @user_same := 1 WHERE ...

更好的是,这避免了对变量的需要。

IF (EXISTS SELECT * FROM ...)

另外,不要使用构造IN ( SELECT ... );通常最好使用WHERE EXISTS ( SELECT * FROM ... ) 或JOIN ... ON ...。

(修复后可能会出现更多问题。)

【讨论】:

    【解决方案2】:

    另一种简单的方法。

    您应该在 ; 分隔符之间添加任何符号,以便将 MySQL 语法适当地转换为原始 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
    

    由于破折号符号,此示例有效。

    【讨论】:

      猜你喜欢
      • 2016-07-07
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 2015-01-05
      • 2020-11-11
      • 2015-10-28
      相关资源
      最近更新 更多