Peter
Peter

Reputation: 11

Grails - How to implement a foreign key relationship not using an id column?

Have been trying to look for an answer for hours, but have so far not managed to come up with an adequate solution, so I'm hoping someone here might have some more experience with Grails, and implementing custom relationships in it.

My problem is that I have two classes:

Company
OrderConfig

OrderConfig contains two references to Company. 1 for a consignee, 1 for a shipper. I implemented the relationship (see below), and everything looks dandy, with my order_config table containing both a consignee_company_id column and a shipper_company_id column.

However, I do not want or need a company_id column at all. I would prefer using the CompanyName as the identifying column. How can I prevent Grails from automatically adding the id column to the company table, and instead use the companyName column as the primary key, thus ensuring my order_config table will be generated containing the companyName and not the company_id?

I tried embedding the company in the orderconfig class, and wrestled with all kinds of other options including the mappings, however I ran into trouble with each and every one of them.

Thanks in advance!

My code so far:

class OrderConfig {
 static hasMany = [consignee:Company, shipper:Company]

  String toString() {
    return "${consignee}"
 }

    static constraints = {
      consignee (blank:false, maxSize:10)
      shipper (blank:false, maxSize:10)
  }

  Company consignee
  Company shipper
}




class Company {
    String toString() {
    return "${companyName}"
 }

static constraints = {
    companyName(blank:false, maxSize:10)
    companyAddress1(blank:false, maxSize:40)
    companyAddress2(blank:false, maxSize:40)
    companyAddress3(blank:false, maxSize:40)
    companyAddress4(blank:false, maxSize:40)
    companyZipCode(blank:false, maxSize:36)
    companyCountry(blank:false, maxSize:36)
}

  String companyName
  String companyAddress1
  String companyAddress2
  String companyAddress3
  String companyAddress4
  String companyZipCode
  String companyCountry
}

Upvotes: 1

Views: 5569

Answers (1)

hvgotcodes
hvgotcodes

Reputation: 120198

You need to look at the advanced GORM config options in the documentation, in section 5.5.2.1

There is an example

class Person {
  String firstName
  static hasMany = [addresses:Address]
  static mapping = {
      table 'people'
      firstName column:'First_Name'
      addresses joinTable:[name:'Person_Addresses', key:'Person_Id', column:'Address_Id']
  }
}

there you can see how they are specifying the key and column name on which to do the stuff.

Edit -- I reread your question: if you are asking how to change it so Company has an assigned id, you might be able to do something like this example

static mapping = {
    id generator:'assigned', column:'column_name',name:'propertyName'
}

where more options are available here: http://www.grails.org/doc/1.3.x/ref/Database%20Mapping/id.html

Upvotes: 1

Related Questions