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

Spring Boot整合H2数据库启动失败问题求助

Spring Boot启动报错:Ingredient表id无主键/唯一约束导致外键创建失败

问题说明

正在通过《Spring in Action》学习Spring,第三章关系数据库章节按步骤操作后,项目启动时出现ApplicationContext错误,核心原因是执行schema.sql第6条添加外键的SQL语句失败,H2数据库提示Ingredient表的id字段无主键或唯一约束。

报错信息

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-09-06T15:27:34.553+03:00 ERROR 7068 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'designTacoController' defined in file [C:\Users\Artem\IdeaProjects\taco-cloud\taco-cloud\target\classes\tacos\web\DesignTacoController.class]: Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'ingredientRepository': Unsatisfied dependency expressed through method 'setDialect' parameter 0: Error creating bean with name 'jdbcDialect' defined in class path resource [org/springframework/boot/autoconfigure/data/jdbc/JdbcRepositoriesAutoConfiguration$SpringBootJdbcConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcDialect' parameter 0: Error creating bean with name 'dataSourceScriptDatabaseInitializer' defined in class path resource [org/springframework/boot/autoconfigure/sql/init/DataSourceInitializationConfiguration.class]: Failed to execute SQL script statement #6 of file [C:\Users\Artem\IdeaProjects\taco-cloud\taco-cloud\target\classes\schema.sql]: alter table Ingredient_Ref add foreign key (ingredient) references Ingredient(id)
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-6.0.11.jar:6.0.11]
    ...(省略重复栈帧)
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: 未找到约束"PRIMARY KEY | UNIQUE (ID)"
Constraint "PRIMARY KEY | UNIQUE (ID)" not found; SQL statement:
alter table Ingredient_Ref add foreign key (ingredient) references Ingredient(id) [90057-214]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:632) ~[h2-2.1.214.jar:2.1.214]
    ...(省略重复栈帧)

Process finished with exit code 0

项目pom.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
          http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>sia</groupId>
    <artifactId>taco-cloud</artifactId>
    <version>0.0.3-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>taco-cloud</name>
    <description>Taco Cloud Example</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.1.2</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>
            UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>
            UTF-8</project.reporting.outputEncoding>
        <java.version>19</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>htmlunit-driver</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- tag::springDataJdbc[] -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jdbc</artifactId>
        </dependency>
        <!-- end::springDataJdbc[] -->
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <dependency>
            <groupId>javax.validation</groupId>
            <artifactId>validation-api</artifactId>
            <version>2.0.1.Final</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>

</project>

解决方案

  1. 检查并修正schema.sql中Ingredient表的创建语句
    确保Ingredient表的id字段被声明为主键或唯一约束,示例正确语句:

    CREATE TABLE Ingredient (
        id VARCHAR(4) NOT NULL PRIMARY KEY,
        name VARCHAR(25) NOT NULL,
        type VARCHAR(10) NOT NULL
    );
    

    外键关联的字段必须是主键或具备唯一约束,H2数据库才允许创建外键关联。

  2. 确认SQL脚本执行顺序
    保证schema.sql中先创建Ingredient表,再执行创建Ingredient_Ref表或添加外键约束的语句,避免因表未创建或约束未存在导致报错。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 13:05:57