【发布时间】:2019-05-28 01:44:50
【问题描述】:
我正在开发一个使用 schema.sql 函数初始化数据库的 Spring Boot JPA 应用程序。但是,当它遇到函数定义时,我收到一个错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #13 of URL [file:/C:/Users/Kelly%20Marchewa/workspace/Auth/auth/bin/main/schema.sql]: CREATE OR REPLACE FUNCTION admin.add_user(new_username VARCHAR, new_password VARCHAR, role_name VARCHAR) RETURNS int AS $$ DECLARE ret_role_id INTEGER; nested exception is org.postgresql.util.PSQLException: Unterminated dollar quote started at position 120 in SQL CREATE OR REPLACE FUNCTION admin.add_user(new_username VARCHAR, new_password VARCHAR, role_name VARCHAR) RETURNS int AS $$ DECLARE ret_role_id INTEGER. Expected terminating $$
函数定义:
CREATE OR REPLACE FUNCTION admin.add_user(new_username VARCHAR, new_password VARCHAR, role_name VARCHAR) RETURNS int AS $$
DECLARE
ret_role_id INTEGER;
ret_user_id INTEGER;
BEGIN
ret_role_id = (SELECT id FROM admin.role WHERE role_title = role_name);
INSERT INTO admin.app_user (username, password) VALUES
(new_username, crypt(new_password, gen_salt('bf', 8)));
ret_user_id = (SELECT id FROM admin.app_user WHERE username = new_username);
INSERT into admin.role(user_id, role_id) VALUES (ret_user_id, ret_role_id);
RETURN ret_user_id;
END;
$$ LANGUAGE plpgsql;
我可以从“psql”CLI 客户端很好地运行这个脚本。我必须进行哪些更改才能使其与 Spring Boot JPA 应用程序/JDBC 驱动程序一起使用?
谢谢。
【问题讨论】:
-
只需删除 $$ LANGUAGE plpgsql PostgreSQL 将识别该语言。
-
@SimonMartinelli 删除 $$ LANGUAGE plpgsql 恐怕不起作用;我收到了同样的错误。也没有将它与 $$ 对一起删除;我收到了
ERROR: syntax error at or near "DECLARE"。谢谢。
标签: postgresql jdbc spring-data-jpa