Grails:获取;字段XXX不';“没有默认值”;

Grails:获取;字段XXX不';“没有默认值”;,grails,dns,many-to-many,default-value,sqlexception,Grails,Dns,Many To Many,Default Value,Sqlexception,我有一个奇怪的错误,这是由两个域类之间的多对多关系引起的。课程如下。当我尝试保存NoteParagation对象时,不会立即出现错误: NoteParagraph np = new NoteParagraph(number: noteNumber, content: noteText) note.addToChildParagraphs(np).save() 但是,在下一个.save()操作(在一个无关对象上)中,我得到以下错误: Field 'note_paragraph_id' doesn

我有一个奇怪的错误,这是由两个域类之间的多对多关系引起的。课程如下。当我尝试保存NoteParagation对象时,不会立即出现错误:

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'
    }
}
我已尝试按照其他线程的建议删除并重新创建数据库,但没有任何帮助。

修复:

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'
    }
}
当您尝试将NoteParaguation添加到Note时,NoteParaguation实例不是持久的(它已创建,但尚未保存在db中)

尝试更改:

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


唉,没有骰子。同样的错误也发生了,现在只出现在您建议编辑的行上:Note段落中的content字段,您输入了多少内容长度?默认情况下,grails的大小非常小。
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()