我正在尝试在我的数据库中插入两个条目(一个用户和一个照片),其中照片属于用户,但是当我将用户保存到数据库时出现错误
列“user_id”不能为空
照片.groovy:
class Photo {
Boolean active = 1
Boolean approved = 0
Date dateAdded = new Date()
String filterName
String senderIp
String originalUrl
String editedUrl
User user
static belongsTo = [User]
static mapping = {
active defaultValue: 1
approved defaultValue: 0
user column: 'user_id'
}
static constraints = {
originalUrl maxSize: 512
editedUrl maxSize: 512
senderIp maxSize: 15
}
}
用户.groovy
class User {
String address
String number
Date birthday
String city
String complement
String cpf
String guardianCpf
String guardianName
String email
String firstName
String lastName
String mobile
String neighborhood
String phone
String rg
String sex
String state
String zipcode
static hasMany = [photos: Photo]
static constraints = {
address maxSize: 256
complement maxSize: 32
city maxSize: 64
complement nullable: true, maxSize: 32
cpf maxSize: 11, unique: true
guardianCpf maxSize: 11, nullable: true
guardianName maxSize: 256, nullable: true
email email: true, blank: false
firstName maxSize: 128
lastName maxSize: 128
mobile maxSize: 12
neighborhood maxSize: 128
phone maxSize: 12
rg maxSize: 9, unique: true
sex maxSize: 1
state maxSize: 2
zipcode maxSize: 9
}
}
用户服务.groovy
@Transactional
class UserService {
def grailsApplication
def registerUser(String address, String number, Date birthday, String city, String complement,
String cpf, String guardianCpf, String email, String firstName,
String lastName, String mobile, String neighborhood, String phone,
String rg, String sex, String state, String zipcode, String filterName, String originalUrl, String editedUrl, String senderIp) {
def status = 0
def content = ""
def u = new User(
address : address,
number : number,
birthday : birthday,
city : city,
complement : complement,
cpf : cpf,
email : email,
firstName : firstName,
lastName : lastName,
mobile : mobile,
neighborhood : neighborhood,
phone : phone,
rg : rg,
sex : sex,
state : state,
zipcode : zipcode,
guardianCpf : guardianCpf)
//I EDITED THIS PART OF THE CODE, TO SOLVE THE CODE SEE BELOW
if (u.validate()){
def p = new Photo(
filterName : filterName,
originalUrl : originalUrl,
editedUrl : editedUrl,
senderIp : senderIp,
user : u
)
if (p.validate()){
try{
u.addToPhotos(p).save(failOnError:true)
status = 1
content = "SUCCESS"
}
catch(e){
status = 0
content = grailsApplication.config.serviceErrors.databaseError
}
}
else{
status = 0
content = grailsApplication.config.serviceErrors.invalidUserData + ": n"
p.errors.allErrors.each {
content += it.toString() + "'"
}
}
}
else {
status = 0
content = grailsApplication.config.serviceErrors.invalidUserData + ": n"
u.errors.allErrors.each {
content += it.toString() + "'"
}
}
return [status: status, content: content]
}
}
有人能帮我吗?
编辑:现在我在添加照片之前保存用户,现在我收到错误:
seviranaembalagem.User 条目中的空 id(发生异常后不要刷新会话)
try{
u.save(failOnError:true)
u.addToPhotos(p)
p.save(flush: true)
status = 1
content = "SUCCESS"
}
catch(e){
status = 0
content = grailsApplication.config.serviceErrors.databaseError
}
EDIT2:在 UserService.groovy 使用此代码解决了问题:
if (u.validate()){
u.save()
def p = new Photo(
filterName : filterName,
originalUrl : originalUrl,
editedUrl : editedUrl,
senderIp : senderIp,
user : u
)
if (p.validate()){
try{
p.save(flush:true)
status = 1
content = "SUCCESS"
}
catch(e){
status = 0
content = grailsApplication.config.serviceErrors.databaseError + e
}
}
else{
status = 0
content = grailsApplication.config.serviceErrors.invalidUserData + ": n"
p.errors.allErrors.each {
content += it.toString() + "'"
}
}
}