我有一个简单的 Restfull 应用程序。有用。但是,当我添加 .sql 文件时 - 该应用程序无法运行。 我添加 schema.sql
CREATE EXTENSION IF NOT EXISTS plpgsql WITH SCHEMA pg_catalog;
CREATE TABLE country (
id SERIAL PRIMARY KEY NOT NULL ,
county_name CHAR (30) NOT NULL UNIQUE );
CREATE TABLE city (
id SERIAL PRIMARY KEY NOT NULL ,
city_name CHAR (30) NOT NULL,
country_id INTEGER REFERENCES country,
UNIQUE (city_name, country_id));
CREATE TABLE street (
id SERIAL PRIMARY KEY NOT NULL,
street_name CHAR (30) NOT NULL,
city_id INTEGER REFERENCES city,
building_number CHAR(10),
UNIQUE (street_name, city_id));
和data.sql
INSERT INTO country (county_name) VALUES ('Ukraine');
INSERT INTO country (county_name) VALUES ('France');
INSERT INTO country (county_name) VALUES ('Italy');
INSERT INTO country (county_name) VALUES ('German');
INSERT INTO country (county_name) VALUES ('England');
应用程序属性
spring.jpa.database=postgresql
spring.datasource.platform=postgres
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=create-drop
spring.database.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/ubr
spring.datasource.username=postgres
spring.datasource.password=1
server.port=8080
第一次运行后 - 它没有错误,但第二次运行后我有 a stacktrace with many exeptions .我做错了什么?
最佳答案
您正在混合两种不同的功能。
spring.jpa.hibernate.ddl-auto=create-drop
在您希望 Hibernate 自动更新架构时运行。
schema.sql
和data.sql
管理是 Spring Boot 的一个特性。 doc is pretty explicit about JPA usage
If you want to use the schema.sql initialization in a JPA app (with Hibernate) then ddl-auto=create-drop will lead to errors if Hibernate tries to create the same tables. To avoid those errors set ddl-auto explicitly to "" (preferable) or "none". Whether or not you use ddl-auto=create-drop you can always use data.sql to initialize new data.
关于sql-server - 当我添加 sql 脚本时,Spring Boot 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38422031/