【发布时间】:2026-01-14 19:15:01
【问题描述】:
我不得不用 H2 内存数据库替换我的 PostgreSQL 数据库,我的迁移开始失败。创建表脚本运行但插入失败。
ChangeSet classpath:database/changelog/create-default-user.xml::createUserTable::ethero ran successfully in 7ms
Change Set classpath:database/changelog/create-default-user.xml::insertDefaultUser::ethero failed. Error: Column "username" not found; SQL statement:
错误堆栈跟踪
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set classpath:database/changelog/create-default-user.xml::insertDefaultUser::ethero:
Reason: liquibase.exception.DatabaseException: Column "username" not found; SQL statement:
INSERT INTO usr ("username", "password") VALUES ('admin', 'password') [42122-200] [Failed SQL: (42122) INSERT INTO usr ("username", "password") VALUES ('admin', 'password')]
创建表脚本
create table user_authorities (username varchar(255) not null , authorities varchar(255));
create table usr (username varchar(255) not null , password varchar(255), image_url varchar(255))
插入用户脚本
INSERT INTO usr ("username", "password") VALUES ('admin', 'password');
INSERT INTO user_authorities ("username", "authorities") VALUES ('admin', 'ADMIN');
INSERT INTO user_authorities ("username", "authorities") VALUES ('admin', 'USER');
Spring 启动配置
spring:
profiles:
active: dev
datasource:
url: jdbc:h2:mem:pc;DB_CLOSE_DELAY=-1;
username: sa
password: sa
driver-class-name: org.h2.Driver
jpa:
generate-ddl: true
hibernate:
ddl-auto: update
show-sql: true
liquibase:
change-log: classpath:database/changelog-master.xml
url: jdbc:h2:mem:pc;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
Spring Boot 版本:2.4.3
【问题讨论】:
-
INSERT语句使用带引号的列名,在这种情况下,每个列名必须与表中定义的列的大小写(大写/小写)匹配。您确实没有在create table语句中使用带引号的列名,所以我猜测username是作为大写列标识符创建的。您可以尝试INSERT INTO usr ("USERNAME", "PASSWORD") VALUES ('admin', 'password');或INSERT INTO usr (username, password) VALUES ('admin', 'password');来测试我的猜测。
标签: java spring-boot h2 liquibase