约束“PRIMARY KEY|未找到UNIQUE(ID)”;在SQL语句中使用Spring Tool Suite 4 IDE

y1aodyip  于 2023-08-04  发布在  Spring
关注(0)|答案(1)|浏览(139)

这是实际误差

Failed to execute SQL script statement #6 of URL [file:/C:/Users/DELL/IdeaProjects/taco-cloud-1/bin/main/schema.sql]: alter table Ingredient_Ref add foreign key (ingredient) references Ingredient(id); nested exception is org.h2.jdbc.JdbcSQLSyntaxErrorException: Constraint "PRIMARY KEY | UNIQUE (ID)" not found; SQL statement:
alter table Ingredient_Ref add foreign key (ingredient) references Ingredient(id) [90057-214]

字符串
这是我的SQL代码。请问有什么语法错误吗,或者我漏掉了什么?

create table if not exists Taco_Order (
    id identity,
    delivery_Name varchar(50) not null,
    delivery_Street varchar(50) not null,
    delivery_City varchar(50) not null,
    delivery_State varchar(2) not null,
    delivery_Zip varchar(10) not null,
    cc_number varchar(16) not null,
    cc_expiration varchar(5) not null,
    cc_cvv varchar(3) not null,
    placed_at timestamp not null
);

create table if not exists Taco (
    id identity,
    name varchar(50) not null,
    taco_order bigint not null,
    taco_order_key bigint not null,
    created_at timestamp not null
);

create table if not exists Ingredient_Ref (
    ingredient varchar(4) not null,
    taco bigint not null,
    taco_key bigint not null
);

create table if not exists Ingredient (
    id varchar(4) not null,
    name varchar(25) not null,
    type varchar(10) not null
);

alter table Taco
    add foreign key (taco_order) references Taco_Order(id);
alter table Ingredient_Ref
    add foreign key (ingredient) references Ingredient(id);


它的目的是与其他代码(html,java)一起运行,作为一个从h2数据库获取这些数据的web应用程序。

oalqel3c

oalqel3c1#

在创建引用约束之前,必须向表中添加主键(或唯一)约束。
您也不应该在现代版本的H2中使用identity作为数据类型,该语法不受支持,并且仅在少数兼容模式下有效,与历史版本的H2的兼容性有限。
您需要在Taco_OrderTaco表的定义中将id identity替换为id bigint generated by default as identity primary key,还需要在Ingredient_RefIngredient表的定义中将id varchar(4) not null替换为id varchar(4) primary key

相关问题