问题
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
Caused by MappingException: Foreign key (FK_ib9w9pn893cwi1dkk84qs31bx:recovery_setup [division, peid,onesol_pe_names_division,onesol_pe_names_peid])) must have same number of columns as the referenced primary key (ONESOL_pe_names [division,pe_id])
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"?
回答1:
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]
...
}
来源:https://stackoverflow.com/questions/31167194/foreign-key-fk-must-have-same-number-of-columns-as-the-referenced-primary-key