You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot集成测试无法识别Docker中PostgreSQL表的问题求助

Fixing "relation 'data_source' does not exist" in Spring Boot Integration Tests

Hey there, let's troubleshoot why your integration test is throwing that PostgreSQL relation error while your REST API runs perfectly. Here are the most common issues and actionable fixes:

1. Verify the Test Database Has the data_source Table

First things first: confirm the table actually exists in the database your tests are connecting to.

  • Connect to your PostgreSQL container using psql:
    psql -h localhost -p 5432 -U postgres postgres
    
  • Run this command to check for the table:
    \d data_source
    

If the table doesn't show up, your test environment isn't initializing the schema correctly.

2. Fix Test Data Source Initialization Configuration

Your test application.properties uses deprecated initialization properties (for Spring Boot 2.5+):

  • Replace these lines:
    spring.datasource.initialization-mode=always
    spring.datasource.initialize=true
    
  • With the current standard:
    spring.sql.init.mode=always
    

Also, ensure your test resources have the correct init.sql (or that the Docker-initiated schema is accessible by your tests). Since your Docker container only runs init.sql on first launch, if you've recreated the container without the script, the table might be missing.

3. Explicitly Specify the Database Schema

Even though your schema is public, Hibernate might not be targeting it correctly in tests. Add this to your test application.properties:

spring.jpa.properties.hibernate.default_schema=public

Or update your entity class to explicitly set the schema:

@Entity
@DynamicUpdate
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "data_source", schema = "public") // Add schema here
public class DataSourceEntity {
    // ... your fields
}

4. Update Deprecated Naming Strategy

Your current naming strategy is outdated for newer Hibernate versions. Replace:

spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy

With:

spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

Since you're already using @Table(name = "data_source"), this won't change your table names, but it eliminates potential configuration conflicts.

5. Ensure Test Context Loads Correct Configuration

Double-check that your integration test is loading the right application context. Ensure @SpringBootTest is pointing to your main application class if needed:

@SpringBootTest(classes = YourMainApplicationClass.class)
@AutoConfigureMockMvc
public class TrainingDataServiceIT {
    // ... your test code
}

Also, make sure no other test configurations (like @TestConfiguration beans) are overriding your data source settings.


内容的提问来源于stack exchange,提问作者gary

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 21:42:48