We got the following error: null, while trying to run this SQL script

We got the following error: null, while trying to run this SQL script

我在新的 Scala Play-Slick 项目上 运行 激活器 运行 时从 Play Evolutions 收到上述错误。我使用的是 Postgres 服务器版本 9.3 和最新的 JDBC 驱动程序 9.4。我的 1.sql 文件如下所示:

# schema

# --- !Ups

CREATE TABLE country (
    id BIGINT,
    name VARCHAR(100),
    iso2 CHAR(2),
    modified TIMESTAMP DEFAULT now(),
    PRIMARY KEY (id)
);

CREATE OR REPLACE FUNCTION update_modified()
RETURNS TRIGGER AS $$
BEGIN
    NEW.modified = now();
    RETURN NEW;
END;
$$ language 'plpgsql';

CREATE TRIGGER trigger_country_modified BEFORE UPDATE ON country FOR EACH ROW EXECUTE PROCEDURE update_modified();

# --- !Downs

DROP TABLE country CASCADE;

DROP FUNCTION update_modified_column;

通过反复试验,我发现 Evolutions 不能很好地理解原生 Postgres 触发器函数 update_modified。我该如何解决或规避这个问题?

Play evolution 插件将您的 .sql 文件拆分为一系列以分号分隔的语句,然后再对数据库逐个执行。

由于您在函数 update_modified() 代码中使用了分号,因此您必须通过输入两次来转义它 ;;。见下文:

CREATE OR REPLACE FUNCTION update_modified()
RETURNS TRIGGER AS $$
BEGIN
    NEW.modified = now();;
    RETURN NEW;;
END;;
$$ language 'plpgsql';