1

How do I map a GORM association where the foreign key is not a PK of the other table?

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).


EDIT: The answer by @tim_yates below seems to be partially working.

Since supplier.supplier_id is not the PK of the supplier table, therefore it is possible for supplier.supplier_id to be duplicated.

In fact, the key of the supplier table is the tuple (supplier.supplier_id, supplier.partner_id). How do I model this constraint?

ryanprayogo
  • 11,587
  • 11
  • 51
  • 66

1 Answers1

1

This similar question seems to show you should be able to add (using advanced Gorm Mapping):

  class Ad {
    Partner partner
    Supplier supplier
    String details

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

Though I've not tested this...

Community
  • 1
  • 1
tim_yates
  • 167,322
  • 27
  • 342
  • 338