SQL 语句 "CREATE TABLE TRIP..." 中的语法错误

Syntax error in SQL statement "CREATE TABLE TRIP..."

我在尝试为 PostgresSql 使用 flyway 和休眠创建 table 时发现语法错误时遇到问题。 Trip class 与其他 class 没有关系(目前)。 我已经成功地为其他 2 个 classes Purchase 和 User 创建了,但是这个只是给出了错误。

错误信息:

[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 3.003 s <<< FAILURE! - in org.studentnr.backend.service.UserServiceTest [ERROR] org.studentnr.backend.service.UserServiceTest.testCreateUser Time elapsed: 0.004 s <<< ERROR! java.lang.IllegalStateException: Failed to load ApplicationContext Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'flywayInitializer' defined in class path resource [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration.class]: Invocation of init method failed; nested exception is org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException: Migration V1.0__createDB.sql failed

SQL State : 42000 Error Code : 42000 Message : Syntax error in SQL statement "CREATE TABLE TRIP (ID BIGINT GENERATED BY DEFAULT AS IDENTITY, TITLE VARCHAR(255) NOT NULL, DESCRIPTION VARCHAR(255), COST INTEGER NOT NULL, LOCATION VARCHAR(124) NOT NULL, DEPARTURE DATE NOT NULL, RETURNING DATE NOT NULL, PRIMARY KEY (ID))

我的飞行路线sql脚本:

create sequence hibernate_sequence start with 1 increment by 1;

create table user_roles (user_email varchar(255) not null, roles varchar(255));

create table users (email varchar(255) not null, firstname varchar(50) not null, middle_name 
varchar(50), surename varchar(50) not null, password varchar(255) not null, address 
varchar(128) not null, postal_code varchar(124) not null, enabled boolean not null, primary 
key (email));

create table purchase (id bigint generated by default as identity, booked_date date not 
null, user_email varchar(255) not null, trip_id bigint not null, primary key (id));

create table trip (id bigint generated by default as identity, title varchar(255) not null, 
description varchar(255), cost integer not null, location varchar(128) not null, departure 
date not null, returning date not null, primary key (id))


alter table user_roles add constraint FKs9rxtuttxq2ln7mtp37s4clce foreign key (user_email)         
references users;

alter table purchase add constraint FKlqrv1aj0pon999jbi5esfpe4k foreign key (user_email) 
references users;

这是我的旅行实体:

package org.studentnr.backend.entities;

import javax.persistence.*;
import javax.validation.constraints.*;
import java.time.LocalDate;

@Entity
public class Trip {

 @Id @GeneratedValue
 private Long id;

 @NotBlank
 @Size(max=255)
 private String title;

 //@NotBlank //TODO: Can be blank???
 @Size(max=255)
 private String description;

 //@Min(0) TODO: remember to add 'check (cost>0)' to flyway to avoid using negative values
 @NotNull
 private Integer cost;

 @NotBlank
 @Size(max = 124)
 private String location;

 @NotNull
 @Future   
 private LocalDate departure;

 @NotNull
 @Future  
 private LocalDate returning;



public Trip(){
}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getTitle() {
    return title;
}

public void setTitle(String title) {
    this.title = title;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public int getCost() {
    return cost;
}

public void setCost(int cost) {
    this.cost = cost;
}

public String getLocation() {
    return location;
}

public void setLocation(String location) {
    this.location = location;
}

public LocalDate getDepartureDate() {
    return departure;
}

public void setDepartureDate(LocalDate departureDate) {
    this.departure = departureDate;
}

public LocalDate getReturnDate() {
    return returning;
}

public void setReturnDate(LocalDate returnDate) {
    this.returning = returnDate;
}



}

您的脚本中有两个错误:

  1. create trip 语句末尾缺少 ;
  2. returning 是保留关键字。您必须引用它 "returning" date - 但如果您找到不同的名称,那么在长 运行 中会更容易。

如果这两个问题得到纠正,脚本 works

我还会为外键约束使用更有意义的名称。