执行DDL alter table if exists任务时发生java错误。城市
错误Error executing DDL "alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house" via JDBC Statement"
我不知道如何解决它,我已经在寻找解决方案,但我检查了我的数据库和实体,一切都是正确的。我自己从头开始创建数据库。我在Postgresql工作。添加了错误日志
性质
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给我的代码:
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;
# 1 楼答案
嗯,试试看
顺便说一句
update
更改为none
。您应该自己管理模式李># 2 楼答案
你不应该使用单向@OneToMany关联。尝试添加
去城市班
另外,你确定你需要一对多的联系吗?从一个家到另一个城市,而不是相反?城市可能有许多房屋,但房屋属于特定的城市