我正在尝试将Spring Boot应用程序部署到Heroku。它使用RemoteMysl提供的远程数据库。但是,在构建应用程序时,它将创建数据库中除一个表之外的所有表。我在Heroku应用程序日志中收到以下错误。
由于:java.sql.SQLSyntaxErrorException:您的错误 SQL语法;检查与您的MySQL服务器相对应的手册 在删除时在'groups(id)附近使用正确语法的版本 级联'在第1行
组模型
package com.itsfive.back.model;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import org.springframework.web.multipart.MultipartFile;
import com.itsfive.back.model.audit.DateAudit;
@Entity
@Table(name = "groups")
public class Group extends DateAudit{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
@Size(max = 30)
private String name;
@Size(max = 160)
private String description;
@OneToOne
@JoinColumn(name = "created_by_id", nullable = false)
private User created_by;
private String coverPhoto;
public User getCreated_by() {
return created_by;
}
public void setCreated_by(User created_by) {
this.created_by = created_by;
}
public String getCoverPhoto() {
return coverPhoto;
}
public void setCoverPhoto(String coverPhoto) {
this.coverPhoto = coverPhoto;
}
public Group(@NotBlank @Size(max = 30) String name, @Size(max = 160) String description,User created_by) {
super();
this.name = name;
this.description = description;
this.created_by = created_by;
}
public Group() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String groupName) {
this.name = groupName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public User getCreatedBy() {
return created_by;
}
public void setCreatedBy(User user) {
this.created_by = user;
}
}
答案 0 :(得分:1)
好吧,我认为您正在使用Mysql 8.0,那么此问题是由于您在表名上使用了Mysql
之类的groups
保留关键字。
您可以看看SQL reserved words for Mysql 8.0,然后有两个选择
1.-将groups
表重命名为users_group
或所需的表名。
@Table(name = "users_group")
public class Group extends XXXXX { ... }
2.-强制使用groups
表名。
如果您使用的是JPA,则可以使用双引号进行转义:
@Table(name = "\"groups\"")
public class Group extends XXXXX { ... }
如果您使用的是Hibernate本机API,则可以使用反引号将其转义:
@Table(name = "`groups`")
public class Group extends XXXXX { ... }