-1
조건이 충족 될 때만 테이블에 삽입하는 트리거를 만들고 싶습니다. IF/BEGIN/END와 WHERE의 다양한 조합을 사용해 보았지만 Django는 매번 SQL 구문 오류를 반환합니다.Django migration 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 오류를 포함하도록 편집했습니다! 그것은 학교 데이터베이스 프로젝트를위한 것이고 django와 raw SQL을 필요로합니다. – treehugeer