my problem database mapping , don't working. i've done this tutorial.
class comment { string comment date datecreated // predefined names grails filled automatically date lastupdated // predefined names grails filled automatically user user // delete comment feedback if feedback item deleted static belongsto=[feedback:feedback] static mapping = { feedback column: 'comment_feedback_id', jointable: false } static constraints = { comment (blank:false, nullable: false, size:5..500) user (nullable: true) // comments allowed without user } string tostring(){ if (comment?.size()>20){ return comment.substring(0,19); } else return comment; }} class feedback { string title string feedback date datecreated // predefined names grails filled automatically date lastupdated // predefined names grails filled automatically // relationship other classes user user static hasmany=[comments:comment] static mapping = { comments column: 'feedback_comment_id', jointable: false } // constrains defined static static constraints ={ title(blank:false, nullable: false, size:3..80) feedback(blank:false, nullable:false, size:3..500) user(nullable:false) }} class user { string name string email string webpage static constraints = { name (blank:false, nullable:false, size:3..30, matches:"[a-za-z1-9_]+") email (email:true) webpage (url:true) } string tostring(){ return name; } } when try delete user connected feedback/ comment, error:
cannot delete or update parent row: foreign key constraint fails (
guestbook.comment, constraintfk_mxoojfj9tmy8088avf57mpm02foreign key (user_id) referencesuser(id))
what should mapping like?
you have multiple problems domain design, first remove user comment ,as user have comment feedback. if still wish keep design, define belongsto user,in both comment , feedback.
try this...
add hasone feedback user
class user { string name string email string webpage hasone = [feedback:feedback ] static constraints = { name (blank:false, nullable:false, size:3..30, matches:"[a-za-z1-9_]+") email (email:true) webpage (url:true) } string tostring(){ return name; } } add feedback belongsto user , cascade on delete comment
class feedback { string title string feedback date datecreated // predefined names grails filled automatically date lastupdated // predefined names grails filled automatically // relationship other classes user user static belongsto = [user] static hasmany=[comments:comment] static mapping = { comments cascade: 'all-delete-orphan',column: 'feedback_comment_id', jointable: false } // constrains defined static static constraints ={ title(blank:false, nullable: false, size:3..80) feedback(blank:false, nullable:false, size:3..500) user(nullable:false) }} simply remove user comment
class comment { string comment date datecreated // predefined names grails filled automatically date lastupdated // predefined names grails filled automatically //user user // delete comment feedback if feedback item deleted /static belongsto=[user,feedback:feedback] static belongsto=[feedback:feedback] static mapping = { feedback column: 'comment_feedback_id', jointable: false } static constraints = { comment (blank:false, nullable: false, size:5..500) //user (nullable: true) // comments allowed without user } string tostring(){ if (comment?.size()>20){ return comment.substring(0,19); } else return comment; }}
Comments
Post a Comment