如果存在task.city,则执行DDL alter table时出错 [英] Error executing DDL alter table if exists task.city
问题描述
错误Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement"
我不知道如何解决,我已经在寻找解决方案,但是我检查了数据库和Entity,一切都正确.我是从头开始创建数据库的.我在Postgresql中工作.添加了错误日志.
The error Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement"
I Don't understand how to solve it, I was already looking for a solution, but I check my database and Entity, everything is correct. I created the database from scratch myself. I work in Postgresql. Added the error log.
属性
spring.datasource.url=jdbc:postgresql://localhost:5432/innotechnum
spring.datasource.username=***
spring.datasource.password=***
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.database=postgresql
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
spring.jpa.open-in-view= true
spring.jpa.show-sql=true
城市
@Data
@Entity
@Table(name = "city", schema = "task")
public class City {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@Column(name = "id_region", nullable = false)
private Integer id_region;
@Column(name = "name", nullable = false)
private String name;
}
房子
@Data
@Entity
@Table (name = "house", schema = "task")
public class House {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH
})
@JoinColumn(name = "id")
private Set<City> city;
@OneToMany(mappedBy = "house")
private Set<Contract> contract;
@Column(name = "id_landlord", nullable = false)
private Long id_landlord;
@Column(name = "outside", nullable = false)
private String outside;
@Column(name = "rooms", nullable = false)
private Integer rooms;
@Column(name = "price", nullable = false)
private Double price;
@Column(name = "description", nullable = false)
private String description;
}
日志:
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
at
...
这是Postgresql给我的代码:
This is the code that Postgresql gives me:
CREATE TABLE task.city
(
id integer NOT NULL DEFAULT nextval('task.city_id_seq'::regclass),
id_region integer NOT NULL,
name character varying(250) COLLATE pg_catalog."default" NOT NULL,
CONSTRAINT city_pkey PRIMARY KEY (id)
)
TABLESPACE pg_default;
ALTER TABLE task.city
OWNER to root;
推荐答案
嗯,尝试
public class House {
...
@ManyToOne(optional = false)
@JoinColumn(name = "city_id", referencedColumnName = "id")
private Set<City> city;
...
}
btw,
- 不应在实体类中使用@Data lombok
- spring.jpa.hibernate.ddl-auto = update可以将
update
更改为none
.您应该自己管理架构.
- should not use @Data lombok in an entity class
- spring.jpa.hibernate.ddl-auto=update can change
update
tonone
. You should manage schema on your own.
这篇关于如果存在task.city,则执行DDL alter table时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!