SqL 脚本插入未找到列

问题描述 投票:0回答:1

这是我的sql脚本:

-- Create an in memory database for the tools rental project.
-- This is expected to be replaced with an actual database
--
-- Create Schema for use
CREATE SCHEMA IF NOT EXISTS Tools_R_Us_Schema;
USE Tools_R_Us_Schema;

-- Create the tool type table for listing the tool types
CREATE TABLE IF NOT EXISTS Tool_Type (
    type varchar(20) NOT NULL PRIMARY KEY
);

-- Create the vendor brands table for listing the brands
CREATE TABLE IF NOT EXISTS Vendors (
    brand varchar(30) NOT NULL PRIMARY KEY
);

-- Create the tools table for listing the tools
CREATE TABLE IF NOT EXISTS Tools (
    code varchar(10) NOT NULL PRIMARY KEY,
    type varchar(20) NOT NULL REFERENCES Tool_Type(type),
    brand varchar(30) NOT NULL REFERENCES Vendors(brand)
);

-- Create the charge table based on tool type
CREATE TABLE IF NOT EXISTS Tools_Charges (
    id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
    type varchar(20) NOT NULL REFERENCES Tool_Type(type),
    daily_charge decimal(5,2),
    weekday_charge bit,
    weekend_charge bit,
    holiday_charge bit
);

-- Create the holiday day falls on table
CREATE TABLE IF NOT EXISTS Holiday_Days_Of_Week (
    day_of_the_week varchar(15) NOT NULL PRIMARY KEY
);

-- Create the allowed frequencies of holiday table
CREATE TABLE IF NOT EXISTS Holiday_Frequency (
    frequency varchar(20) NOT NULL PRIMARY KEY
);

-- Create the holidays table for checking if it is a holiday
CREATE TABLE IF NOT EXISTS Holidays (
    name varchar(20) NOT NULL PRIMARY KEY,
    holiday_month int,
    holiday_day int,
    day_of_the_week varchar(15) REFERENCES Holiday_Days_Of_Week(day_of_the_week),
    frequency varchar(20) REFERENCES Holiday_Frequency(frequency)
);

-- Create the list of valid statuses for rental
CREATE TABLE IF NOT EXISTS Tool_Status (
    status varchar(10) NOT NULL PRIMARY KEY
);

-- Create Rental table for tracking when a tool is rented
CREATE TABLE IF NOT EXISTS Tool_Rentals (
    id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
    code varchar(10) NOT NULL REFERENCES Tools(code),
    type varchar(20) NOT NULL REFERENCES Tool_Type(type),
    brand varchar(30) NOT NULL REFERENCES Vendors(brand),
    rental_days int NOT NULL,
    checkout_date date NOT NULL,
    due_date date,
    charge_days int NOT NULL,
    due decimal(10,2) NOT NULL,
    daily_charge decimal(5,2) NOT NULL,
    pre_discount_charge decimal(10,2) NOT NULL,
    discount_percent decimal(3,3),
    discount_amount decimal(10,2),
    final_charge decimal(10,2) NOT NULL,
    status varchar(10) NOT NULL REFERENCES Tool_Status(status)
);

-- Populate the tool types
INSERT INTO Tool_Type (type)
    VALUES ('ChainSaw'),
           ('JackHammer'),
           ('Ladder');

-- Populate the vendor brands table
INSERT INTO Vendors (brand)
    VALUES ('Stihl'),
           ('Werner'),
           ('DeWalt'),
           ('Ridgid');

-- Populate the Tools table
INSERT INTO Tools (code, type, brand)
    VALUES ('CHNS', 'ChainSaw', 'Stihl'),
           ('LADW', 'Ladder', 'Werner'),
           ('JAKD', 'JackHammer', 'DeWalt'),
           ('JAKR', 'JackHammer', 'Ridgid');

-- Populate the charge table
INSERT INTO Tools_Charges (type, daily_charge, weekday_charge, weekend_charge, holiday_charge)
    VALUES ('Ladder', 1.99, 1, 1, 0),
           ('ChainSaw', 1.49, 1, 0, 1),
           ('JackHammer', 2.99, 1, 0, 0);

-- Populate the days of the week a holiday can fall on
INSERT INTO Holiday_Days_Of_Week (day_of_the_week)
   VALUES ('Monday');

-- Populate the frequencies for holidays allowed
INSERT INTO Holiday_Frequency (frequency)
    VALUES ('First');

-- Populate the holidays table
INSERT INTO Holidays (name, holiday_month, holiday_day, day_of_the_week, frequency)
    VALUES ('Independence Day', 7, 4, null, null),
           ('Labor Day', null, null, 'Monday', 'First');

-- Populate the valid statuses for rentals
INSERT INTO Tool_Status (status)
    VALUES ('ACTIVE'),
           ('CLOSED');

这是错误:

Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Column "TYPE" not found; SQL statement:

那么痕迹有:

org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #14 of file [/Users/paqpaq/Dropbox/WORK/CF/pa1024/build/resources/main/data.sql]: INSERT INTO Tools (code, type, brand) VALUES ('CHNS', 'ChainSaw', 'Stihl'), ('LADW', 'Ladder', 'Werner'), ('JAKD', 'JackHammer', 'DeWalt'), ('JAKR', 'JackHammer', 'Ridgid')

插入 Tools 或 Tools_Charges 时出现错误,找不到“类型”列。 我尝试过更改名称,但这并不能解决问题。 我已经通过 sql 验证器运行它并且它验证了。 所以我不知道哪里出了问题。

如果有影响的话,这是在 H2 数据库的 Intellij 设置中。 它正在工作,然后停止了,我唯一改变的是在 Tool_Type 上我添加了下划线,因为它奇怪地在 H2 控制台中添加了它。

想法?

java sql intellij-idea h2
1个回答
0
投票

看来问题不在于 SQL。 相反,它是使用我作为 @Entity 创建的 Java POJO。 我使用 @OneToOne 作为外键引用,尽管所有名称都相同,但它表示找不到该列。 将 @OneToOne 更改为 @Column 修复了错误。 数据库仍然保留着关键关系。 我读到的所有内容都表明我所拥有的应该有效,但事实并非如此。 至少我还能继续前进。

© www.soinside.com 2019 - 2024. All rights reserved.