创建 SQL table 时出错。丹 类
Error when creatin SQL table. Eban classes
我尝试创建一个 Transaction
SQL table,其中包含来自另外两个 table 的两个外键:User
和 Car
.
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
table脚本
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。所以只要选择一个不是保留字的名字即可。
我尝试创建一个 Transaction
SQL table,其中包含来自另外两个 table 的两个外键:User
和 Car
.
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
table脚本
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。所以只要选择一个不是保留字的名字即可。