简体   繁体   English

Grails GORM映射FK而不是另一个表的PK

[英]Grails GORM mapping FK that is not the PK of the other table

How do I map a GORM association where the foreign key is not a PK of the other table? 我如何映射外键不是另一个表的PK的GORM关联?

I have the following schema: 我有以下架构:

CREATE TABLE `supplier` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `partner_id` int(11) NOT NULL,
  `supplier_id` int(11) NOT NULL
  PRIMARY KEY (`id`)
)

CREATE TABLE `ad` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `partner_id` int(11) NOT NULL,
  `supplier_id` int(11) NOT NULL,
  `ad_id` varchar(30) NOT NULL,
  `ad_details` text NOT NULL
  PRIMARY KEY (`id`)
)

The FK relationship is between ad.supplier_id and supplier.supplier_id ( NOT supplier.id ). 该FK关系之间ad.supplier_idsupplier.supplier_id (NOT supplier.id )。


EDIT: The answer by @tim_yates below seems to be partially working. 编辑:下面@tim_yates的答案似乎部分起作用。

Since supplier.supplier_id is not the PK of the supplier table, therefore it is possible for supplier.supplier_id to be duplicated. 由于supplier.supplier_id不是PK的supplier表,因此有可能supplier.supplier_id被复制。

In fact, the key of the supplier table is the tuple ( supplier.supplier_id , supplier.partner_id ). 实际上, supplier表的键是元组( supplier.supplier_idsupplier.partner_id )。 How do I model this constraint? 如何建模此约束?

This similar question seems to show you should be able to add (using advanced Gorm Mapping ): 这个类似的问题似乎表明您应该能够添加(使用高级Gorm映射 ):

  class Ad {
    Partner partner
    Supplier supplier
    String details

    static mapping = {
      supplier column:'supplier_id'
      details type:"text"
    }
  }

Though I've not tested this... 虽然我还没有测试过...

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

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