gpt4 book ai didi

grails - Grails:获取 “Field XXX doesn'具有默认值”

转载 作者:行者123 更新时间:2023-12-02 15:54:40 25 4
gpt4 key购买 nike

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

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

但是,在下一个.save()操作(在不相关的对象上)上,我得到以下错误stacktrace:
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):
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'
}
}

当您尝试将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()

关于grails - Grails:获取 “Field XXX doesn'具有默认值”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984621/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com