约束FK防御的问题

时间:2018-12-24 17:06:34

标签: sql spring-boot h2

我的问题是我正在尝试配置h2数据库并为此使用一些脚本。 创建表时应用程序启动失败。 清单和stacktrace在下面。它是供内部开发使用的,此脚本是使用IDEA自动生成的。我对SQL不好,所以我认为这可能是儿童错误。但是不知何故,我检查了很多站点,文章等,但仍然不知道,我在哪里错了。

create table Table1
(
    record_ID int not null,
    dateTime datetime not null,
    timerName varchar(14) not null
        constraint Table1_Table2_timerName_fk
        references Table2(timerName)
            on update cascade,
    status smallint,
    primary key (record_ID, spotName, partIdentString)
);

create table Table2
(
    timerName varchar(14) not null
        constraint Table2_pk
        primary key nonclustered,
    center varchar(2),
    station varchar(4)  not null,
    cell varchar(6)  not null,
    place varchar(10) not null
);

然后我正在尝试运行应用,但失败

Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #1 of file [****\target\classes\sql\schema\my_dbo_Table1.sql]: create table Table1 ( record_ID int not null, dateTime datetime not null, timerName varchar(14) not null constraint 
Table1_Table2_timerName_fk references Table2(timerName) on update cascade, status smallint, primary key (record_ID, spotName, partIdentString) ); nested exception is org.h2.jdbc.JdbcSQLException: Table "Table2" not found; SQL statement:
create table Table1 ( record_ID int not null, dateTime datetime not null, timerName varchar(14) not null constraint Table1_Table2_timerName_fk references Table2(timerName) on update cascade, status smallint, primary key (record_ID, spotName, partIdentString) ) [42102-197]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:493)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:238)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:192)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:92)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:83)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:366)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:311)
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:134)
    ... 67 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLException: Table "Table2" not found; SQL statement:
create table Table1 ( record_ID int not null, dateTime datetime not null, timerName varchar(14) not null constraint Table1_Table2_timerName_fk references Table2(timerName) on update cascade, status smallint, primary key (record_ID, spotName, partIdentString) ) [42102-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.command.ddl.AlterTableAddConstraint.tryUpdate(AlterTableAddConstraint.java:204)
    at org.h2.command.ddl.AlterTableAddConstraint.update(AlterTableAddConstraint.java:78)
    at org.h2.command.ddl.CommandWithColumns.createConstraints(CommandWithColumns.java:88)
    at org.h2.command.ddl.CreateTable.update(CreateTable.java:122)
    at org.h2.command.CommandContainer.update(CommandContainer.java:102)
    at org.h2.command.Command.executeUpdate(Command.java:261)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:233)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:205)
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:472)
    ... 79 common frames omitted

1 个答案:

答案 0 :(得分:0)

  

嵌套的异常是org.h2.jdbc.JdbcSQLException:未找到表“ Table2”;

您首先尝试创建的

Table1具有对Table2的引用约束。

要解决该错误,请在脚本中反转CREATE TABLE命令:必须先创建Table2,然后再创建Table1。

NB:DateTime是sqlserver的数据类型。您应该避免将列命名为保留字,否则可能会导致各种令人惊讶的行为。