【发布时间】:2020-05-26 07:41:58
【问题描述】:
该项目目前在 spring-boot 2.27 上(它是相应的 spring-data-jdbc 版本)在 maven 中升级版本并运行时,我收到以下信息:
bad SQL grammar [INSERT INTO "PLAYLIST" ("allocated_funds", "file_uri", "received") VALUES (?, ?, ?)]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "PLAYLIST" does not exist
org.springframework.data.relational.core.conversion.DbActionExecutionException: Failed to execute DbAction.InsertRoot(entity=Playlist(id=null, fileUri=gs://playlists/success.csv, received=2020-05-26T09:34:45.778327, allocatedFunds=20000))
我正在使用 flyway 来管理我的数据库,它正在被测试执行:
09:34:43.868 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
09:34:43.897 [main] INFO o.f.c.i.database.DatabaseFactory - Database: jdbc:postgresql://localhost:32883/test (PostgreSQL 12.3)
09:34:43.947 [main] INFO o.f.core.internal.command.DbValidate - Successfully validated 4 migrations (execution time 00:00.019s)
09:34:43.970 [main] INFO o.f.c.i.s.JdbcTableSchemaHistory - Creating Schema History table "public"."flyway_schema_history" ...
09:34:44.010 [main] INFO o.f.core.internal.command.DbMigrate - Current version of schema "public": << Empty Schema >>
INFO o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 1 - init
INFO o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 2 - playlist entry
INFO o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 3 - splits
INFO o.f.core.internal.command.DbMigrate - Migrating schema "public" to version 4 - allocated funds
INFO o.f.core.internal.command.DbMigrate - Successfully applied 4 migrations to schema "public" (execution time 00:00.255s)
实体看起来像:
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.relational.core.mapping.Column;
import org.springframework.data.relational.core.mapping.Table;
@Data
@Builder
@AllArgsConstructor
@Table("PLAYLIST")
public class Playlist implements Serializable {
@Id
private Long id;
@Column("file_uri")
private String fileUri;
private LocalDateTime received;
@Column("allocated_funds")
private BigDecimal allocatedFunds;
}
创建脚本如下:
CREATE TABLE PLAYLIST(id BIGSERIAL PRIMARY KEY, file_uri VARCHAR(400) NOT NULL, received TIMESTAMP NOT NULL);
ALTER TABLE PLAYLIST ADD COLUMN allocated_funds DECIMAL(10,2) NOT NULL;
WRT datetime 或 bigdecimal 处理是否存在一些潜在变化或缺少 ID?
- 更新 - 添加存储库代码
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import treeline.entities.Playlist;
public interface PlaylistRepository extends CrudRepository<Playlist, Long> {
@Query("SELECT * FROM Playlist p WHERE p.file_uri = :fileUri")
Playlist findByFileUri(@Param("fileUri") final String fileUri);
}
【问题讨论】:
-
引用的对象名称区分大小写,因此您可能有一个表
playlist(小写),但您插入的PLAYLIST不存在。禁用引用,或尝试删除@Table注释,或将表格放入小写。 -
表格播放列表的创建语句已提供为大写
-
是的,但是它是不带引号的,并且不带引号的对象名称不区分大小写,这意味着 PostgreSQL 会将其存储为 小写 (其他数据库系统例如将不带引号的名称存储在大写)。这意味着
PLAYLIST实际上是playlist,而"PLAYLIST"将不匹配。 -
在
@Table中改为小写并已排序
标签: java spring-boot spring-data-jdbc