java - SQL 语句“CREATE TABLE TRIP...”中的语法错误
问题描述
尝试使用 flyway 和 hibernate for PostgresSql 创建表时,我在发现语法错误时遇到问题。Trip 类与其他类(目前)没有关系。我已经成功地为其他 2 个类 Purchase 和 User 创建了,但是这个只是给出了错误。
错误信息:
[错误] 测试运行:1,失败:0,错误:1,跳过:0,经过时间:3.003 s <<< FAILURE!- 在 org.studentnr.backend.service.UserServiceTest [错误] org.studentnr.backend.service.UserServiceTest.testCreateUser 经过时间:0.004 秒 <<< 错误!java.lang.IllegalStateException:无法加载 ApplicationContext 原因:org.springframework.beans.factory.BeanCreationException:在类路径资源 [org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration$FlywayConfiguration 中定义名称为“flywayInitializer”的 bean 创建错误.class]: init 方法调用失败;嵌套异常是 org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException: Migration V1.0__createDB.sql failed
SQL 状态:42000 错误代码:42000 消息:SQL 语句中的语法错误“CREATE TABLE TRIP(ID BIGINT 由默认身份生成,TITLE VARCHAR(255) NOT NULL,DESCRIPTION VARCHAR(255),COST INTEGER NOT NULL,LOCATION VARCHAR( 124)不为空,出发日期不为空,返回日期不为空,主键(ID))
我的flyway 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;
}
}
解决方案
您的脚本中有两个错误:
- 语句
;
末尾有一个缺失。create trip
returning
是保留关键字。您将不得不引用它"returning" date
- 但从长远来看,如果您找到不同的名称会更容易。
如果这两件事都得到纠正,脚本就可以工作
我还会为外键约束使用更有意义的名称。
推荐阅读
- swift - 是否可以在 swift 中以编程方式按 ENTER?
- java - 我无法从 Android Studio 中的 URL 获取 HTML 代码
- angular - 在 Angular 中刷新页面。Socket.io 问题
- spring-boot - JUnit测试用例在使用时没有返回正确的值,然后返回条件
- amazon-web-services - 带有 docker hub 映像的 ECS FARGATE TASK 定义
- javascript - 检查本地文件是否已更改
- r - 带有表格 ggplot 编码的森林图
- c# - 如何在整个应用程序中创建和注入用户特定的单例类?
- c++ - 我正在尝试实现典型的最低成本路径问题。但是我无法处理给定的 Struct 类和 ENUM 类型我该如何处理?
- php - 在 PHP 中使用 domDocument 获取部分文本