本文介绍了外键(FK_必须与引用的主键具有相同的列数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理遗留数据库,其中表A由3个复合键组成,表B由2个复合键组成,这两个复合键与表A中的两个复合键相同

OnesolPeNames

  class OnesolPeNames implements Serializable {

静态映射= {
表ONESOL_pe_names

id合成:[division,peid]

列{
分栏:''分区',长度:8,sqlType:char
peid列:'pe_id',长度:12,sqlType:char
peNameU列:'pe_name_u',长度:50,sqlType: char
}
}

static hasMany = [recoverySetups:RecoverySetup]

...

RecoverySetup

  class RecoverySetup实现了Serializable {

static mapping = {
tablerecovery_s etup

id复合:[division,peid,orgkey]

列{
分栏:'division',长度:10 ,sqlType:char
peid列:'peid',长度:12,sqlType:char
orgkey列:'org_key',长度:8,sqlType:char

oneSolName列:['division','peid']


}
}

static belongsTo = [oneSolName:OnesolPeNames]

....

我试图像这样访问OnesolPeNames 。

recoverySetup.onesolPeNames.peNameU。



我收到以下错误: b
$ b

我将oneSolName列设置为 ['division','peid'] ,其中 onesol_pe_names_division,onesol_pe_names_peid 来自哪里?



难道我的组合键是PK,但是也扮演着被称为派生身份的FK的角色?

解决方案

我有一个类似的问题,我以这种方式解决了映射表:

  class RecoverySetup实现了Serializable {

static mapping = {
tablerecovery_setup
id composite:[division, peid,orgkey]
列{
orgkey列:'org_key',长度:8,sqlType:char

oneSolName {
列名:'division'
列名:'peid'
}
}
oneSolName可更新:false,可插入:false
}

静态belongsTo = [oneSolName:OnesolPeNames]
...
}


I'm working with a legacy database where Table A consist of 3 composite keys and Table B consist of 2 composite keys which are the same as two of the composite keys from Table A

OnesolPeNames

class OnesolPeNames implements Serializable {

    static mapping = {
        table "ONESOL_pe_names"

        id composite: ["division", "peid"]

        columns{
            division column: 'division', length: 8, sqlType: "char"
            peid column: 'pe_id', length: 12, sqlType: "char"
            peNameU column: 'pe_name_u', length: 50, sqlType: "char"
        }
    }

    static hasMany = [ recoverySetups : RecoverySetup]

    ....

RecoverySetup

class RecoverySetup implements Serializable {

    static mapping = {
        table "recovery_setup"

        id composite: ["division", "peid", "orgkey"]

        columns {
            division column: 'division', length: 10, sqlType: "char"
            peid column: 'peid', length: 12, sqlType: "char"
            orgkey column: 'org_key', length: 8, sqlType: "char"

            oneSolName column: ['division', 'peid']


        }
    }

    static belongsTo = [oneSolName: OnesolPeNames]

    ....

I'm trying to access OnesolPeNames like so.

recoverySetup.onesolPeNames.peNameU.

I'm getting the following error

I'm setting oneSolName column to ['division', 'peid'], where is onesol_pe_names_division,onesol_pe_names_peid coming from?

Could it be that my composite keys are a part of PK but also plays role as a FK which is known as "Derived Identities"?

解决方案

I had a similar issue and I resolved mapping table in this way:

class RecoverySetup implements Serializable {

    static mapping = {
        table "recovery_setup"
        id composite: ["division", "peid", "orgkey"]
        columns {
            orgkey column: 'org_key', length: 8, sqlType: "char"

            oneSolName {
                column name: 'division'
                column name: 'peid'
            }
        }
        oneSolName updateable: false, insertable: false
    }

    static belongsTo = [oneSolName: OnesolPeNames]
    ...
}

这篇关于外键(FK_必须与引用的主键具有相同的列数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 02:34