我在psql中遇到触发器问题需要检查表中是否已存在某些值(发送者,接收者,主题),如果它们存在,我只需要更新一个值(subject = RE:+ subject)in该行,如果没有那么我需要插入新行。
CREATE OR REPLACE FUNCTION check_theme()
RETURNS TRIGGER
AS $$
DECLARE e BOOLEAN;
BEGIN
e := EXISTS(
SELECT *
FROM message
WHERE sender = NEW.sender
AND reciever = NEW.reciever
AND subject = NEW.subject
);
IF NOT e THEN
INSERT INTO message(sender, reciever, subject, text)
VALUES(NEW.sender, NEW.reciever, NEW.subject, NEW.text);
ELSE
UPDATE message
SET subject = 'Re: ' || NEW.subject
WHERE sender = NEW.sender
AND reciever = NEW.reciever
AND subject = NEW.subject;
END IF;
RETURN NEW;
END;
$$
LANGUAGE plpgsql;
CREATE TRIGGER theme_check
BEFORE INSERT OR UPDATE
ON message
FOR EACH ROW EXECUTE PROCEDURE check_theme();
因此,在我想插入已经具有相同发送方值的新行之后,接收方和主题触发器将进入不定式循环。
我做错了什么?
答案 0 :(得分:0)
几天后我发现我对触发器工作原理的了解是错误的。所以我的错误是我添加了INSERT功能但是如果没有相同的发件人,收件人和标题的邮件,INSERT也会完成那就是我总是得到不定循环的原因。 所以可行的代码是belove
<YOUR_VIEW_PACKAGE.CustomTextView
android:id="@+id/edt_my_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:drawableStartCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
app:drawableEndCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
app:drawableTopCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
app:drawableBottomCompat="@drawable/your_vector_drawable" <!-- vector drawable -->
/>