写真がユーザーに属しているデータベースに 2 つのエントリ (ユーザーと写真) を挿入しようとしていますが、ユーザーをデータベースに保存するとエラーが発生します。
列 'user_id' を null にすることはできません
Photo.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
}
}
UserService.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 が null (例外が発生した後にセッションをフラッシュしないでください)
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() + "'"
}
}
}