【问题标题】:Grails Database MappingGrails 数据库映射
【发布时间】:2015-07-17 09:13:53
【问题描述】:

我的问题是我的数据库映射,但我无法正常工作。 我已经完成了this 教程。

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;
}
}

当我尝试删除与反馈/评论相关联的用户时,我收到错误消息:

无法删除或更新父行:外键约束失败 (guestbook.comment,约束FK_mxoojfj9tmy8088avf57mpm02 外键 (user_id) 引用 user (id))

映射应该是什么样的?

【问题讨论】:

  • 蒂姆,您不应该期望其他人通读您链接的整个教程然后帮助您。通过总结本教程中理解您的问题并相应地更新您的问题所必需的部分来帮助其他人。然后他们也更有可能帮助你。只是一个建议。

标签: mysql grails mapping


【解决方案1】:

您的域设计存在多个问题,首先将用户从评论中删除,因为用户已经从反馈中获得了评论。如果您仍希望保留该设计,请在 CommentFeedback 中定义 belongsToUser

试试这个...

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;
}
}

添加Feedback 属于User 并级联删除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)
}}

只需从Comment 中删除User

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;
}}

【讨论】:

    猜你喜欢
    • 2017-03-05
    • 2015-04-29
    • 2011-05-27
    • 2014-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多