grails - 无法在Bootstrap中插入数据

标签 grails gorm

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

下表是对应于域的表

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!'
    }
}

最佳答案

您使用空的photoUrl“photoUrl:”“创建新商品。
定义网址或添加约束-photoUrl(nullable:true)

关于grails - 无法在Bootstrap中插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33006162/

相关文章:

javascript - 在Grails中将JSON对象呈现为AJAX调用

grails - 如何在不替换 Jenkins 中每个构建的旧 war 的情况下产生新 war ?

grails - 数据库功能查询到Gorm字段的结果

grails - Grails 3.1.0.M2数据库逆向工程

grails - 从GORM获取对象集

jquery - 将下拉列表值传递给 Grails 中的 jquery

Grails - 如何从表单调用 Controller 方法

grails - Grails上的Spring Security和带有领域的Tomcat Security

hibernate - Hibernate在Database First开发中找不到表

grails - 在Grails中使用多个数据源