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

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

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 中工作。添加了错误日志。

属性配置

  1. spring.datasource.url=jdbc:postgresql://localhost:5432/innotechnum
  2. spring.datasource.username=***
  3. spring.datasource.password=***
  4. spring.datasource.driver-class-name=org.postgresql.Driver
  5. spring.jpa.database=postgresql
  6. spring.jpa.hibernate.ddl-auto=update
  7. spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
  8. spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
  9. spring.jpa.open-in-view=true
  10. spring.jpa.show-sql=true

City 类

  1. @Data
  2. @Entity
  3. @Table(name = "city", schema = "task")
  4. public class City {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. @Column(name = "id")
  8. private Long id;
  9. @Column(name = "id_region", nullable = false)
  10. private Integer id_region;
  11. @Column(name = "name", nullable = false)
  12. private String name;
  13. }

House 类

  1. @Data
  2. @Entity
  3. @Table(name = "house", schema = "task")
  4. public class House {
  5. @Id
  6. @GeneratedValue(strategy = GenerationType.IDENTITY)
  7. @Column(name = "id")
  8. private Long id;
  9. @OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
  10. @JoinColumn(name = "id")
  11. private Set<City> city;
  12. @OneToMany(mappedBy = "house")
  13. private Set<Contract> contract;
  14. @Column(name = "id_landlord", nullable = false)
  15. private Long id_landlord;
  16. @Column(name = "outside", nullable = false)
  17. private String outside;
  18. @Column(name = "rooms", nullable = false)
  19. private Integer rooms;
  20. @Column(name = "price", nullable = false)
  21. private Double price;
  22. @Column(name = "description", nullable = false)
  23. private String description;
  24. }

日志:

  1. 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
  2. at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
  3. at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
  4. ...

这是 PostgreSQL 给出的代码:

  1. CREATE TABLE task.city
  2. (
  3. id integer NOT NULL DEFAULT nextval('task.city_id_seq'::regclass),
  4. id_region integer NOT NULL,
  5. name character varying(250) COLLATE pg_catalog."default" NOT NULL,
  6. CONSTRAINT city_pkey PRIMARY KEY (id)
  7. )
  8. TABLESPACE pg_default;
  9. ALTER TABLE task.city
  10. 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

  1. spring.datasource.url=jdbc:postgresql://localhost:5432/innotechnum
  2. spring.datasource.username=***
  3. spring.datasource.password=***
  4. spring.datasource.driver-class-name=org.postgresql.Driver
  5. spring.jpa.database=postgresql
  6. spring.jpa.hibernate.ddl-auto=update
  7. spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL10Dialect
  8. spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
  9. spring.jpa.open-in-view= true
  10. spring.jpa.show-sql=true

City

  1. @Data
  2. @Entity
  3. @Table(name = &quot;city&quot;, schema = &quot;task&quot;)
  4. public class City {
  5. @Id
  6. @GeneratedValue(strategy= GenerationType.IDENTITY)
  7. @Column(name = &quot;id&quot;)
  8. private Long id;
  9. @Column(name = &quot;id_region&quot;, nullable = false)
  10. private Integer id_region;
  11. @Column(name = &quot;name&quot;, nullable = false)
  12. private String name;
  13. }

house

  1. @Data
  2. @Entity
  3. @Table (name = &quot;house&quot;, schema = &quot;task&quot;)
  4. public class House {
  5. @Id
  6. @GeneratedValue(strategy=GenerationType.IDENTITY)
  7. @Column(name = &quot;id&quot;)
  8. private Long id;
  9. @OneToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
  10. CascadeType.REFRESH
  11. })
  12. @JoinColumn(name = &quot;id&quot;)
  13. private Set&lt;City&gt; city;
  14. @OneToMany(mappedBy = &quot;house&quot;)
  15. private Set&lt;Contract&gt; contract;
  16. @Column(name = &quot;id_landlord&quot;, nullable = false)
  17. private Long id_landlord;
  18. @Column(name = &quot;outside&quot;, nullable = false)
  19. private String outside;
  20. @Column(name = &quot;rooms&quot;, nullable = false)
  21. private Integer rooms;
  22. @Column(name = &quot;price&quot;, nullable = false)
  23. private Double price;
  24. @Column(name = &quot;description&quot;, nullable = false)
  25. private String description;
  26. }

LOGS:

  1. 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
  2. at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:67) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
  3. at org.hibernate.tool.schema.internal.AbstractSchemaMigrator.applySqlString(AbstractSchemaMigrator.java:559) ~[hibernate-core-5.4.20.Final.jar:5.4.20.Final]
  4. at
  5. ...

This is the code that Postgresql gives me:

  1. CREATE TABLE task.city
  2. (
  3. id integer NOT NULL DEFAULT nextval(&#39;task.city_id_seq&#39;::regclass),
  4. id_region integer NOT NULL,
  5. name character varying(250) COLLATE pg_catalog.&quot;default&quot; NOT NULL,
  6. CONSTRAINT city_pkey PRIMARY KEY (id)
  7. )
  8. TABLESPACE pg_default;
  9. ALTER TABLE task.city
  10. OWNER to root;

答案1

得分: 2

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

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

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

英文:

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

  1. @ManyToOne(fetchType = LAZY)
  2. @JoinColumn(name = &quot;house_id&quot;)
  3. 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

嗯,请尝试:

  1. public class House {
  2. ...
  3. @ManyToOne(optional = false)
  4. @JoinColumn(name = "city_id", referencedColumnName = "id")
  5. private Set<City> city;
  6. ...
  7. }

顺便提一下,

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

Hmm, try

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

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:

确定