[英]Mysql Unique constraint over multiple columns
I have this table and, as the code shows, I have marked columns " cnpj, product, proposalNumber " as a unique composed constraint:我有这张表,正如代码所示,我已将列“ cnpj, product, proposalNumber ”标记为唯一的组合约束:
@Table(name = "Proposal", uniqueConstraints = {@UniqueConstraint(columnNames = {"cnpj", "product", "proposalNumber"})})
public class Proposal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, updatable = false, insertable = false)
@JsonProperty("id")
private Long id;
@JsonProperty("cnpj")
@Column(name = "cnpj", nullable = false, length = 14)
private String cnpj;
@JsonProperty("proposalNumber")
@Column(name = "proposalNumber", nullable = false)
private String proposalNumber;
@JsonProperty("product")
@Column(name = "product", nullable = false, length = 100)
private String product;
@JsonProperty("price")
@Column(name = "price", nullable = false)
private BigDecimal price;
@JsonProperty("dueDate")
@Column(name = "dueDate", nullable = false)
private String dueDate;
@JsonProperty("qtyLife")
@Column(name = "qtyLife", nullable = false)
private Integer qtyLife;
@JsonIgnore
@Column(name = "active", nullable = false)
private Boolean active = true;
...
But, checking the DDL or DUMP ain't no unique information...但是,检查 DDL 或 DUMP 并不是没有唯一信息......
CREATE TABLE `proposal` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`active` bit(1) NOT NULL,
`cnpj` varchar(14) NOT NULL,
`due_date` varchar(255) NOT NULL,
`price` decimal(19,2) NOT NULL,
`product` varchar(100) NOT NULL,
`proposal_number` varchar(255) NOT NULL,
`qty_life` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
In addition to this, writing @Column(unique = true)
on the column does not solve the problem either, it creates the unique constraint in the database but only referencing that single column, not the composition ( cnpj, product and proposalNumber ).除此之外,在列上写入
@Column(unique = true)
也不能解决问题,它会在数据库中创建唯一约束,但仅引用该单个列,而不是组合( cnpj、product 和 proposalNumber )。
Any tips?有小费吗?
The problem is solved: nothing wrong with my solution, but I had to change my spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
to spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
The problem is solved: nothing wrong with my solution, but I had to change my
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
to spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.