2

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?

Cheyne
  • 1,964
  • 4
  • 27
  • 43

1 Answers1

0

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
dre-hh
  • 7,840
  • 2
  • 33
  • 44