gpt4 book ai didi

java - 创建 SQL 表时出错。埃类类

转载 作者:行者123 更新时间:2023-11-29 12:20:39 24 4
gpt4 key购买 nike

我尝试创建一个 Transaction SQL 表,其中包含来自另外两个表的两个外键:UserCar
Car.java

@Entity
public class Car extends Model{

@Id
@GeneratedValue
public int id;
@ManyToOne
@JoinColumn(name="user_fk")
public User user;
@OneToOne(cascade=CascadeType.ALL, mappedBy="car")
public Transaction transaction;
}

User.java

@Entity
public class User extends Model{

@Id
@GeneratedValue
public int id;
@OneToMany(cascade=CascadeType.ALL, mappedBy="user") // If we delete a user, all the object belongs to this user are deleted.
public List<Car> cars = new ArrayList<Car>();
@OneToOne(cascade=CascadeType.ALL, mappedBy="user")
public Transaction transaction;
}

Transaction.java

@Entity
@Table(name = "transactions")
public class Transaction extends Model{

@Id
@GeneratedValue
public int id;
@OneToOne
@JoinColumn(name = "user_fk")
public User user;
@OneToOne
@JoinColumn(name = "car_fk")
public Car car;
}

这是生成的transactions表脚本

create table transactions (
id integer not null,
user_fk integer,
car_fk integer,
from timestamp,
to timestamp,
availability boolean,
constraint pk_transactions primary key (id))

alter table transactions add constraint fk_transactions_user_4 foreign key (user_fk) references user (id) on delete restrict on update restrict;
create index ix_transactions_user_4 on transactions (user_fk);
alter table transactions add constraint fk_transactions_car_5 foreign key (car_fk) references car (id) on delete restrict on update restrict;
create index ix_transactions_car_5 on transactions (car_fk);

我收到以下错误:

Syntax error in SQL statement "CREATE TABLE TRANSACTIONS ( ID INTEGER NOT NULL, USER_FK INTEGER, CAR_FK INTEGER, FROM[*] TIMESTAMP, TO TIMESTAMP, AVAILABILITY BOOLEAN, CONSTRAINT PK_TRANSACTIONS PRIMARY KEY (ID)) "; expected "identifier"; SQL statement: create table transactions ( id integer not null, user_fk integer, car_fk integer, from timestamp, to timestamp, availability boolean, constraint pk_transactions primary key (id)) [42001-175] [ERROR:42001, SQLSTATE:42001], while trying to run this SQL script

我做错了什么?

最佳答案

CREATE TABLE TRANSACTIONS ( 
ID INTEGER NOT NULL,
USER_FK INTEGER,
CAR_FK INTEGER,
FROM[*] TIMESTAMP,
TO TIMESTAMP,
AVAILABILITY BOOLEAN,
CONSTRAINT PK_TRANSACTIONS PRIMARY KEY (ID))

错误是使用 FROM 作为列名。 FROM 是保留字,绝对没有理由使用保留字作为列名 http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html 。因此,只需选择一个不是保留字的名称即可。

关于java - 创建 SQL 表时出错。埃类类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29073136/

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