【问题标题】:Grails GORM unique constraintGrails GORM 唯一约束
【发布时间】:2014-02-27 22:16:02
【问题描述】:

我有以下 ListProducts 域对象;

String name
Date lastUpdated
Date dateCreated = new Date()
String lastUpdatedBy
CatLists catLists // 
Product product // has a composite primary key
ListProducts listProducts

static constraints = {
    name nullable: true, blank: true
    catLists nullable: false
    lastUpdatedBy nullable: true
    lastUpdated nullable: true
    dateCreated nullable: false
    product unique: 'listProducts'
}

static mapping = {
    version true
    table 'list_products'
    sort "name"
}

Product 域类具有以下约束,它是一个复合键

FOREIGN KEY (product_fulfillment_sku, product_merchant_id) REFERENCES product(fulfillment_sku, merchant_id)

因为它没有主键,即 ID,因为表因批处理作业而被删除,因此唯一值是 sku 和商家 ID。

我可以在 list_products 表中看到 product_fulfillment_sku、product_merchant_id 和 cat_lists_id 列,但是如何在 grails 域类中表示这些,以便如果产品已经存在则无法添加?

我想要实现的是,一个产品可以在多个 ListProducts 分组中,但每个分组只能出现一次。

针对 Postgres Sql 数据库的 Grails v2.2.1!

有什么想法吗?

J

【问题讨论】:

  • 那么您在此处显示的这个域对象代表一个分组?
  • 正确。所以我重构了代码,以便 catLists 域对象在其列表中添加和删除项目。如果有人删除了该列表,那么 ListProducts 表中的所有引用也会被删除。我从这个博客mrpaulwoods.wordpress.com/2011/02/07/… 中获得了一些灵感

标签: sql hibernate grails grails-orm


【解决方案1】:

所以我得到的解决方法是删除对唯一约束的依赖。这也会阻止并行查找,因为表很大。从Performance Collections获得一些灵感

【讨论】:

    【解决方案2】:

    您可以在看起来像这样的域类中使用唯一约束

    domain1 unique:"domain2"
    

    domain1 和 domain2 都必须是对象而不是集合。

    如果您使用集合,您可以创建另一个可以使用唯一约束的映射类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多