Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm in the middle of a migration to Postgres from MySQL and i'v hit an issue that I just can't solve. I'm sure there is simple answer, but i'm stuck.

The Problem

I have a step model that belongs to a template model, but specifies a foreign key called template_uuid to match on the template tables uuid column.

This all worked fine on MySQL but now does not work on Postgres. Calling step.template would return the relevant template but now it returns nil

Both the uuid column on the template table and the template_uuid column on the step are UUID data types.

Example:

class Step < ActiveRecord::Base
 belongs_to    :template, :foreign_key => :template_uuid

So, I get nil when trying to call the association

step = Step.last
step.template  => nil

But, I can query for the template using the template_uuid column, and it works just fine

Template.where(:uuid => step.template_uuid).first

So .. What am I missing here. The records and UUID's clearly line up so why does this relationship break now that i'm using Postgres. There is no physical foreign key on the database, but that's never mattered before.

Does anyone have any ideas?

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I can't say why it worked before, but as long as you have also a custom primary_key on the associated model (other than :id), you have to provide that either

  belongs_to :template, :primary_key => :uuid, :foreign_key => :template_uuid
share|improve this answer
    
That did it. Thank you! I knew it had to be something small like that –  Cheyne Jul 3 at 0:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.