gpt4 book ai didi

grails - Grails的GORM约束问题

转载 作者:行者123 更新时间:2023-12-02 14:38:37 25 4
gpt4 key购买 nike

我有以下域类:

 class Metric {

String name
float value

static belongsTo = [Person,Corporation]

static indexes = {
name()
}
}

如何添加约束,以便Person,Corporation和name是唯一的?

谢谢。

最佳答案

我认为这应该工作..将此添加到Metric。显然,您可以根据需要忽略可为空的内容。

static constraints = {
name(blank:false)
corporation(nullable:false)
person(nullable:false)

name(unique:['corporation','person'])
}

我使用此集成测试进行了测试,它似乎可以正常工作。
def newCorp = new Corporation(name:"Corporation1")
newCorp.save()
def newPerson = new Person(name:"Person1")
newPerson.save()

def newMetric = new Metric(name:"Metric1",corporation:newCorp,person:newPerson)
newMetric.save()

newMetric = new Metric(name:"Metric1",corporation:newCorp,person:newPerson)
newMetric.save()

assertTrue (Metric.list().size == 1)

关于grails - Grails的GORM约束问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2977191/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com