错误执行 DDL 更改表如果存在 task.city

huangapple go评论72阅读模式
英文:

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" 我不明白如何解决它,我已经寻找了解决方案,但我检查了我的数据库和实体,一切都是正确的。我是从头开始创建的数据库。我在 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

City 类

@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;
}

House 类

@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]
    ...

这是 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;
英文:

The error Error executing DDL &quot;alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house&quot; via JDBC Statement&quot; 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.

Properties

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

City

@Data
@Entity
@Table(name = &quot;city&quot;, schema = &quot;task&quot;)
public class City {

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name = &quot;id&quot;)
    private Long id;

    @Column(name = &quot;id_region&quot;, nullable = false)
    private Integer id_region;
    @Column(name = &quot;name&quot;, nullable = false)
    private String name;
}

house

@Data
@Entity
@Table (name = &quot;house&quot;, schema = &quot;task&quot;)
public class House {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = &quot;id&quot;)
    private Long id;
    
    @OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
            CascadeType.REFRESH
    })
    @JoinColumn(name = &quot;id&quot;)
    private Set&lt;City&gt; city;
    
    @OneToMany(mappedBy = &quot;house&quot;)
    private Set&lt;Contract&gt; contract;

    @Column(name = &quot;id_landlord&quot;, nullable = false)
    private Long id_landlord;
    @Column(name = &quot;outside&quot;, nullable = false)
    private String outside;
    @Column(name = &quot;rooms&quot;, nullable = false)
    private Integer rooms;
    @Column(name = &quot;price&quot;, nullable = false)
    private Double price;
    @Column(name = &quot;description&quot;, nullable = false)
    private String description;
}

LOGS:

org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL &quot;alter table if exists task.city add constraint FKtjrg7h2j3ehgycr3usqjgnc2u foreign key (id) references task.house&quot; 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
	...

This is the code that Postgresql gives me:

CREATE TABLE task.city
(
    id integer NOT NULL DEFAULT nextval(&#39;task.city_id_seq&#39;::regclass),
    id_region integer NOT NULL,
    name character varying(250) COLLATE pg_catalog.&quot;default&quot; NOT NULL,
    CONSTRAINT city_pkey PRIMARY KEY (id)
)

TABLESPACE pg_default;

ALTER TABLE task.city
    OWNER to root;

答案1

得分: 2

你不应该使用单向的 @OneToMany 关联。尝试将以下内容添加到 City 类中:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "house_id")
private House house;

另外,你确定需要从房屋到城市的一对多关联而不是反过来吗?一个城市可以拥有多座房屋,但一座房屋属于特定的城市。

英文:

You should not use unidirectional @OneToMany association. Try to add the

@ManyToOne(fetchType = LAZY)
@JoinColumn(name = &quot;house_id&quot;)
private House house;

to the City class.

P.S. Do you sure you need one-to-many association from house to city but not vice versa? City may have many houses, but house belongs to the particular city.

答案2

得分: 1

嗯,请尝试:

public class House {
  ...
  @ManyToOne(optional = false)
  @JoinColumn(name = "city_id", referencedColumnName = "id")
  private Set<City> city;
  ...
}

顺便提一下,

  • 实体类中不应使用@Data lombok
  • 将 spring.jpa.hibernate.ddl-auto=update 中的 update 改为 none。您应该自行管理模式。
英文:

Hmm, try

public class House {
  ...
  @ManyToOne(optional = false)
  @JoinColumn(name = &quot;city_id&quot;, referencedColumnName = &quot;id&quot;)
  private Set&lt;City&gt; city;
  ...
}

btw,

  • should not use @Data lombok in an entity class
  • spring.jpa.hibernate.ddl-auto=update can change update to none. You should manage schema on your own.

huangapple
  • 本文由 发表于 2020年9月26日 01:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64069005.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定