我在创建 psql 函数时遇到问题,该函数在插入或更新表中的一行之前被称为触发器:“Performance”,它在特定音乐节期间举行音乐表演。
我想要完成的是该函数将检查新的性能 beg_date 和 end_date 是否不会与现有性能发生冲突。限制是在给定的音乐节上一次只能进行一场表演。
表“性能”的结构如下所示:
id
beg_date : timestamp
end_date : timestamp
artist_id
music_concert_id
还有 Music_Festivals:
id
beg_date : timestamp
end_date : timestamp
name
我的功能如下所示:
CREATE OR REPLACE FUNCTION check_valid_performance_date() RETURNS TRIGGER AS $check_date$
BEGIN
IF EXISTS (SELECT * from wystep) THEN
IF EXISTS (SELECT 1 FROM wystep WHERE beg_date > NEW.beg_date AND beg_date < NEW.end_date AND id_music_festival = NEW.id_music_festival LIMIT 1) THEN
RAISE EXCEPTION 'exception 1';
ELSIF EXISTS (SELECT 1 FROM wystep WHERE beg_date < NEW.beg_date AND end_date > NEW.beg_date AND id_music_festival = NEW.id_music_festival LIMIT 1) THEN
RAISE EXCEPTION 'exception 2 ';
ELSIF EXISTS (SELECT 1 FROM wystep WHERE beg_date <= NEW.beg_date AND NEW.end_date <= end_date AND id_music_festival = NEW.id_music_festival LIMIT 1) THEN
RAISE EXCEPTION 'exception 3 ';
END IF;
END IF;
RETURN NEW;
END
$check_date$ LANGUAGE plpgsql;
我的触发器:
CREATE TRIGGER check_valid_performance
AFTER INSERT OR UPDATE OR DELETE ON Performance
FOR EACH ROW EXECUTE PROCEDURE check_valid_performance_date();
但是即使我从空表开始,我也会得到“异常 3”。这个功能有什么问题?