无法在Bootstrap

时间:2015-10-08 03:34:12

标签: grails gorm

在BootStrap中,我在初始化时将数据插入表中。结果可以插入到类别表中的数据中,这在货物表中始终是失败的,这是为什么?货物表插入的数据失败。

以下是与域

对应的表格
package my

class Goods {
    String title
    String description
    BigDecimal price
    String photoUrl
    Category category
}

package my

class Category {

    String categoryName

    static hasMany = [goods:Goods]

    static constraints = {
        categoryName(unique:true)
    }
}

在BootStrap中,我写了:

if (category.save()) {
    println 'new category saved!'
    /*
    def allgoods = [new gdepot2.Goods(title:'Grails',price:20.0,
    description:'Grails Book...',photoUrl:''),
    new gdepot2.Goods(title:'Groovy',price:20.0,
    description:'Groovy Book...',photoUrl:'')
    ]
    allgoods*.category = category
    if(allgoods*.save()){
    println 'all goods saved!'}
    */

    def goods1 = new my.Goods(title:'Grails',price:20.0,
                              description:'Grails Book...',photoUrl:'')
    goods1.category = category
    if (goods1.save()) {
        println 'goods1 saved!'
    }
    def goods2 = new my.Goods(title:'Groovy',price:20.0,
                              description:'Groovy Book...',photoUrl:'')
    goods2.category = category
    if (goods2.save()) {
        println 'goods2 saved!'
    }
}

2 个答案:

答案 0 :(得分:2)

您使用null photoUrl“photoUrl:''”创建新商品。 定义url或添加约束 - photoUrl(nullable:true)

答案 1 :(得分:1)

我thinsk可能有一些约束失败,你可以写else块来查看错误,如

println goods2.errors.allErrors