我有一个现有的mysql数据库表“Projects”有两行: 表:
project_id int(11) AI PK //(this is NOT NULL AUTO_INCREMENT)
name varchar(255)
description varchar(255)
行:
1 test testproject
2 dev devproject
我们希望将其公开为REST CRUD。
所以我创建了一个域bean:
@ToString(includeNames = true, includeFields = true, excludes = 'dateCreated,lastUpdated,metaClass')
@EqualsAndHashCode
class Projects {
int project_id
String name
String description
static constraints = {
name blank:false, nullable:false
}
static mapping = {
table 'projects'
version false
id column:'project_id', sqlType:'int'
project_id insertable:false, updateable:false
}
}
控制器: 我运行了autogenerate-controller并添加了
extends RestfulController {
static responseFormats = ['json', 'xml']
看起来如此:
@Transactional
class ProjectsController extends RestfulController {
static responseFormats = ['json', 'xml']
static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]
def index(Integer max) {
params.max = Math.min(max ?: 10, 100)
response.setHeader("Access-Control-Allow-Origin", "*")
respond Projects.list(params), model:[projectsInstanceCount: Projects.count()]
}
def show(Projects projectsInstance) {
respond projectsInstance
}
def create() {
respond new Projects(params)
}
@Transactional
def save(Projects projectsInstance) {
if (projectsInstance == null) {
notFound()
return
}
if (projectsInstance.hasErrors()) {
respond projectsInstance.errors, view:'create'
return
}
projectsInstance.save flush:true
request.withFormat {
form multipartForm {
flash.message = message(code: 'default.created.message', args: [message(code: 'projects.label', default: 'Projects'), projectsInstance.id])
redirect projectsInstance
}
'*' { respond projectsInstance, [status: CREATED] }
}
}
}
当我发布POST时,我收到错误:
2014-06-14 23:48:07,678 [http-bio-8090-exec-2] ERROR spi.SqlExceptionHelper - Field 'id' doesn't have a default value
Error |
2014-06-14 23:48:07,751 [http-bio-8090-exec-2] ERROR errors.GrailsExceptionResolver - SQLException occurred when processing request: [POST] /passfarm/Projects
Field 'id' doesn't have a default value. Stacktrace follows:
Message: Field 'id' doesn't have a default value
Line | Method
->> 1086 | createSQLException in com.mysql.jdbc.SQLError
我发现了有关类似问题的问题,但在重新创建模式时,它对我不起作用。
感谢您的帮助。