简体   繁体   English

在 Grails 中保存具有表关联的数据

[英]Saving data with table associations in Grails

I have two database tables 'Student' and 'Course'.我有两个数据库表“学生”和“课程”。 I want a one-to-one relation where there is foreign key of 'Course' table in 'Student' table.我想要一个一对一的关系,其中“学生”表中有“课程”表的外键。 The foreign key 'course_id' is appearing in the Student table but whenever I try to save the Student data (after filling form), the data does not get saved and there is no error as well.外键“course_id”出现在学生表中,但每当我尝试保存学生数据(填写表格后)时,数据不会被保存,也没有错误。 I am very new to grails.我对 grails 很陌生。 Please guide me how I can save the data correctly.请指导我如何正确保存数据。

The table structure is as desired but the data is not getting saved to the database.表结构符合要求,但数据未保存到数据库中。 There is no issue with other code parts because the data is getting saved when I remove the association between the two tables.其他代码部分没有问题,因为当我删除两个表之间的关联时,数据会被保存。

Student controller学生 controller

package com.grails

class studentController {
    def save(Student student){
        student.save()
    }
}

Student domain class学生域class

class Student {
    Course course

    String firstName
    String lastName
}

Course domain class课程域class

class Course{
    String course_name
    String duration

    static hasOne = [student: Student]

    //static belongsTo = [student: Student]
}

Note that grails 4 introduced a bug with one-to-one relationships that gives inconsistent behavior based on the owning side of the relationship: https://github.com/grails/grails-core/issues/11606请注意,grails 4 引入了一个具有一对一关系的错误,该错误会根据关系的拥有方给出不一致的行为: https://github.com/grails/grails-core/issues/11606

I'm not sure if this is the same problem you may be seeing without more information on your error, or even if you're on this version of grails.我不确定这是否与您在没有更多错误信息的情况下看到的相同问题,或者即使您使用的是此版本的 grails。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM