Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql Grails-[1:N]关系问题_Mysql_Sql Server_Grails - Fatal编程技术网

Mysql Grails-[1:N]关系问题

Mysql Grails-[1:N]关系问题,mysql,sql-server,grails,Mysql,Sql Server,Grails,我有两个不同的域类,一个用于员工,一个用于部门。它们之间的关系是[1:N],这意味着许多员工可以在一个部门工作,但不能在另一个部门工作。问题是,在Grails在项目运行时从域类创建表之后,对于一名员工,该表的部门Id引用部门表上的Id。例如,对于名为“Peter”的用户,部门id为1 department表还包含部门名称以及部门id 如何在employee表中引用department_id以指向department.name而不是department.id 部门域类: class Departm

我有两个不同的域类,一个用于员工,一个用于部门。它们之间的关系是[1:N],这意味着许多员工可以在一个部门工作,但不能在另一个部门工作。问题是,在Grails在项目运行时从域类创建表之后,对于一名员工,该表的部门Id引用部门表上的Id。例如,对于名为“Peter”的用户,部门id为1

department表还包含部门名称以及部门id

如何在employee表中引用department_id以指向department.name而不是department.id

部门域类:

class Department {
    String name

    static hasMany = [
            employees: Employee
    ]

    static constraints = {
    }

    static mapping = {
        version false
    }

    def String toString() {
        name
    }


}
class Employee {
    String firstName
    String lastName
    String email
    String country
    int born

    static belongsTo = [department: Department]

    static constraints = {
        firstName(blank: false)
        lastName(blank: false)
        born(blank: false)
        email(blank: false, email: true)
        country(blank: false)
    }

    static mapping = {
        version false
    }
}
员工域类:

class Department {
    String name

    static hasMany = [
            employees: Employee
    ]

    static constraints = {
    }

    static mapping = {
        version false
    }

    def String toString() {
        name
    }


}
class Employee {
    String firstName
    String lastName
    String email
    String country
    int born

    static belongsTo = [department: Department]

    static constraints = {
        firstName(blank: false)
        lastName(blank: false)
        born(blank: false)
        email(blank: false, email: true)
        country(blank: false)
    }

    static mapping = {
        version false
    }
}

我需要的是,在Employee表中,在department.name而不是department.id处引用的department\u id列。

我想您需要配置department表的主键是'name'而不是默认的'id'列。Grails随后将使用该名称作为外键

i、 e:

参考(Grails 3.1):