简体   繁体   English

Mysql 多列的唯一约束

[英]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.

相关问题 在多个字段上设置唯一约束 - Putting unique constraint on multiple fields 如何确保对多列(mm_book_id和mm_author_id)应用唯一约束? - How to ensure unique constraint is applied on multiple columns (mm_book_id and mm_author_id )? JPA如何在数据库中为多列中的一个值设置唯一约束 - How to set up unique constraint in database for one value in multiple columns in JPA 多于两列组合的唯一约束 - Unique constraint on combination of more than two columns 是否需要索引具有唯一约束的列? - Do columns with a unique-constraint need to be indexed? 打开连接上的 UCanAccess 异常 - 引用的列上不存在 UNIQUE 约束 - UCanAccess exception on open connection - UNIQUE constraint does not exist on referenced columns 如何使用UCanAccess在两列上创建具有唯一约束的表? - How to CREATE TABLE with unique constraint on two columns using UCanAccess? 在mysql休眠中保存子实体时忽略唯一约束 - Unique constraint Ignored while saving child entity in mysql hibernate 在JPA中区分多个列 - Distinct Over multiple columns in JPA 约束无效:表上没有与外键中的列数和类型相匹配的唯一键或主键约束 - Constraint is invalid: there is no unique or primary key constraint on table that matches the number and types of the columns in the foreign key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM