0
votes

my problem is my database mapping and i don't get it working. I've done this Tutorial.

class Comment {
String comment
Date dateCreated // Predefined names by Grails will be filled automatically
Date lastUpdated // Predefined names by Grails will be filled automatically

User user

// delete a comment for a feedback if the feedback item is 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 are allowed without a 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 by Grails will be filled automatically
Date lastUpdated // Predefined names by Grails will be filled automatically

// relationship to the other classes
User user


static hasMany=[comments:Comment]

static mapping = {
    comments column: 'FEEDBACK_COMMENT_ID', joinTable: false
}

// constrains are defined as 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 I try to delete a User which is connected to a feedback/ a comment, I get an error:

Cannot delete or update a parent row: a foreign key constraint fails (guestbook.comment, CONSTRAINT FK_mxoojfj9tmy8088avf57mpm02 FOREIGN KEY (user_id) REFERENCES user (id))

What should the mapping look like?

1
Tim, you shouldn't expect others to read through the entire tutorial that you linked and then help you. Help the others by summarizing the parts of the tutorial that are necessary to understand your problem and update your question accordingly. Then they will be more likely to help you, too. Just a suggestion. - nils

1 Answers

0
votes

You have multiple problems with the domain design, first remove the user from comment ,as the user already have a comment from feedback. if you still wish to keep that design, then define belongsTo to User,in both Comment and Feedback.

Try this...

Add hasOne Feedback to 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 and cascade on delete for Comment

class Feedback {
String title
String feedback
Date dateCreated // Predefined names by Grails will be filled automatically
Date lastUpdated // Predefined names by Grails will be filled automatically

// relationship to the 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 are defined as 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 from Comment

class Comment {
String comment
Date dateCreated // Predefined names by Grails will be filled automatically
Date lastUpdated // Predefined names by Grails will be filled automatically

//User user

// delete a comment for a feedback if the feedback item is 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 are allowed without a user
}


String toString(){
    if (comment?.size()>20){
        return comment.substring(0,19);
    } else
        return comment;
}}