mysql - Grails 数据库映射

标签 mysql grails mapping

我的问题是我的数据库映射,但我无法让它工作。 我已经完成了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;
}
}

当我尝试删除连接到反馈/评论的用户时,出现错误:

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

映射应该是什么样子?

最佳答案

您在域设计方面存在多个问题,首先将用户从评论中删除,因为该用户已经有来自反馈的评论。如果您仍希望保留该设计,请在 CommentFeedback 中将 belongsTo 定义为 User

试试这个...

hasOne 反馈添加到用户

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

添加反馈属于用户并级联删除评论

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

只需从评论中删除用户

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

关于mysql - Grails 数据库映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31472250/

相关文章:

php - PDO 在 for 循环中失去连接?

ios - 在 RestKit 中混合 NSManagedObjects 和常规对象

java - 为什么当 application.properties 存在时不使用 @PropertySource ?

jquery - Grails:使用Controller中的数据更新GSP中的表

c++ - 将内存数据 ptr 映射到 qt 输入字段

mysql - 如何创建子查询

php - 将MySQL查询的结果分配给字符串

mysql - 将 mysql workbench 连接到 GCE mysql

javascript - grails 中的 CORS - 所有请求都失败了吗?

grails - Grails 中所有 4xx 或 5xx 错误的通用 URLMapping