【问题标题】:Grails: getting "Field XXX doesn't have a default value"Grails:获取“字段 XXX 没有默认值”
【发布时间】:2016-01-25 03:28:24
【问题描述】:

我遇到了一个由两个域类之间的多对多关系引起的奇怪错误。课程如下。当我尝试保存 NoteParagraph 对象时,我不会立即收到错误消息:

NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText)
note.addToChildParagraphs(np).save()

但是,在下一个 .save() 操作(在不相关的对象上)时,我收到以下错误堆栈跟踪:

Field 'note_paragraph_id' doesn't have a default value. Stacktrace follows:
java.sql.SQLException: Field 'note_paragraph_id' doesn't have a default value
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1901)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2113)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2049)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2034)
at org.grails.datastore.gorm.GormStaticApi$_methodMissing_closure2.doCall(GormStaticApi.groovy:102)

这是我的课程:

class NoteParagraph {

    String number
    String content
    Note parentNote

    Date dateCreated
    Date lastUpdated

    static belongsTo = Note

    static hasMany = [
        childNotes: Note
    ]

    static constraints = {
    }

    static mapping = {
        content type:'text'
    }
}

第二个:

class Note {

    Integer noteType
    Integer parentType
    Integer parentId
    String heading

    Date dateCreated
    Date lastUpdated

    static hasMany = [
        childParagraphs: NoteParagraph
    ]

    static constraints = {

    }

    static mapping = {
        heading type:'text'
    }
}

我已尝试按照其他线程的建议删除并重新创建我的数据库,但没有帮助。

【问题讨论】:

  • NoteParagraph中的content字段,你放了多少内容的长度?默认情况下,grails 的大小非常小。

标签: grails dns many-to-many default-value sqlexception


【解决方案1】:

修复:

class NoteParagraph {

    String number
    String content

    Date dateCreated
    Date lastUpdated

    static belongsTo = [parentNote: Note]

    static hasMany = [
        childNotes: Note
    ]

    static constraints = {
    }

    static mapping = {
        content type:'text'
    }
}

当您尝试将 NoteParagraph 添加到 Note 时,NoteParagraph 实例不是持久的(它已创建但尚未保存在 db 中)。

尝试改变:

NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText)
note.addToChildParagraphs(np).save()

到:

note.save(flush: true) //add also this, if note is not persisted
NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText, parentNote: note).save(flush: true)
note.addToChildParagraphs(np).save()

【讨论】:

  • 唉,没有骰子。出现同样的错误,现在只出现在您建议编辑的那一行:
猜你喜欢
  • 2018-07-09
  • 2016-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-28
  • 2015-07-27
  • 1970-01-01
  • 2012-09-25
相关资源
最近更新 更多