我在尝试创建数据时遇到主键约束......
class BootStrap {
def init = { servletContext ->
new ListItPlan(m_id: "248656", plan_id: "12345XX9876543").save()
new ListItPlan(m_id: "209459", plan_id: "12345XX9876543").save()
new ListItPlan(m_id: "248656", plan_id: "56748XXX123933").save()
new ListItPlan(m_id: "209459", plan_id: "56748XXX123933").save()
new Plan(plan_id: "12345XX9876543", p_id_type: "PLAN-ID").save()
new Plan(plan_id: "56748XXX123933", p_id_type: "PLAN-ID").save()
new Cred(m_id: "248656", d_name: "Lorem Ipsum").save()
new Cred(m_id: "209459", d_name: "Ipsum").save()
}
def destroy = { } }
我做了一些研究并对其进行了一些修改。我尝试了不同的东西,但我想我只是不了解如何构建数据。我尝试过这样的事情......
def d1 = new Cred(m_id: "248656", d_name: "Lorem Ipsum").save(failOnError: true)
def ldp1 = new ListItPlan(Cred : d1, plan_id: "12345XX9876543").save(failOnError: true)
...但我不能让它填充表格。请帮忙......
我的模特......
class ListItPlan {
String m_id
String plan_id
static hasMany = [creds : Cred, plans : Plan]
static constraints = {
m_id()
plan_id()
}
}
class Cred {
String m_id
String g_name
static belongsTo = [owner : ListItPlan]
static hasMany = [plans : Plan]
static constraints = {
}
}
class Plan {
String plan_id
String plan_id_type
static belongsTo = [listItPlan : ListItPlan, cred: Cred]
static constraints = {
}
}
如何编写正确的语句以根据模型的结构将数据输入表中?提前谢谢。
答案 0 :(得分:-1)
如果m_id是"主键"在您插入的桌子上,它必须是唯一的。你有两次相同的密钥。
试试这个:
new ListItPlan(m_id: "1", plan_id: "12345XX9876543").save()
new ListItPlan(m_id: "2", plan_id: "12345XX9876543").save()
new ListItPlan(m_id: "3", plan_id: "56748XXX123933").save()
new ListItPlan(m_id: "4", plan_id: "56748XXX123933").save()