gpt4 book ai didi

java - 使用 H2 插入外键时出现问题

转载 作者:行者123 更新时间:2023-11-29 17:54:34 25 4
gpt4 key购买 nike

我正在使用 H2 来测试我的数据库代码。为此,我首先读取一组 SQL 文件并使用 RunScript.execute 执行 SQL。

我真正的数据库是 MySQL,我已经测试了 SQL 脚本以确保它们能够正常工作。问题显然出在 H2 内部。这是一组脚本:

CREATE TABLE stores (
id integer AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
UNIQUE(name),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp
);

CREATE TABLE regions (
id integer AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
UNIQUE(name),
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp
);


CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY store_fk(store_id) REFERENCES stores(id),
FOREIGN KEY region_fk(region_id) REFERENCES regions(id)
);

运行这 3 个脚本来使用 MySQL Workbench 创建表效果很好。通过以下代码从 JUnit 运行它们不会:

Reader storeTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V1__create_stores_table.sql"));

Reader regionTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V2__create_regions_table.sql"));

Reader productTableSql = new InputStreamReader(
DatabaseConnectorTests.class.getResourceAsStream(
"/migrations/V3__create_products_table.sql"));

this.dc = DatabaseConnector.getConnection();
RunScript.execute(dc, storeTableSql);
RunScript.execute(dc, regionTableSql);
RunScript.execute(dc, productTableSql);

我收到以下错误:

org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "CREATE TABLE PRODUCTS (
ID INTEGER AUTO_INCREMENT PRIMARY KEY,
STORE_ID INTEGER NOT NULL,
NAME VARCHAR(255) NOT NULL,
REGION_ID INTEGER NOT NULL,
CREATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UPDATED_AT TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
DELETED_AT TIMESTAMP,
UNIQUE(STORE_ID, NAME),
FOREIGN KEY STORE_FK[*](STORE_ID) REFERENCES STORES(ID),
FOREIGN KEY REGION_FK(REGION_ID) REFERENCES REGIONS(ID)
) "; expected "("; SQL statement:
CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY store_fk(store_id) REFERENCES stores(id),
FOREIGN KEY region_fk(region_id) REFERENCES regions(id)
) [42001-196]

此代码在 MySQL 中运行,我通过以下连接字符串在 MySQL 兼容模式下运行 H2:jdbc:h2:file:~/database;MODE=MySQL。我不知道我还能在这里做什么。这个错误对我来说似乎很奇怪,因为我不知道为什么它会认为我需要另一个 (.

有人知道如何解决这个问题吗?或者对如何为我的测试数据库添加种子有更好的建议?由于我运行的系统受到限制,我无法使用 hibernate 来执行此任务,因此我不得不手动执行很多操作。

谢谢!

最佳答案

来自this帖子,但有更多解释。

无论出于何种原因,H2 中的外键约束都不允许命名约束。解决这个问题的方法就是简单地删除名称:

CREATE TABLE products (
id integer AUTO_INCREMENT PRIMARY KEY,
store_id integer NOT NULL,
name varchar(255) NOT NULL,
region_id integer NOT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
deleted_at timestamp,
UNIQUE(store_id, name),
FOREIGN KEY (store_id) REFERENCES stores(id),
FOREIGN KEY (region_id) REFERENCES regions(id)
);

错误消息非常不清楚且具有误导性。希望这对某人有帮助。

关于java - 使用 H2 插入外键时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48978481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com