views:

232

answers:

0

I cannot get the composite_primary_keys gem to work with my database.

I have the following database tables:

mysql> describe currencies;
+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| curr_code | varchar(3)   | NO   | PRI |         |       |
| curr_name | varchar(255) | YES  |     | NULL    |       |
| symbol    | varchar(255) | YES  |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> describe catalogs_currencies;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| catalog_name    | varchar(255) | NO   | PRI |         |       |
| manufacturer_id | varchar(255) | NO   | PRI |         |       |
| curr_code       | varchar(3)   | NO   | PRI |         |       |
+-----------------+--------------+------+-----+---------+-------+
3 rows in set (0.23 sec)

mysql> describe catalogs;
+-----------------+--------------+------+-----+---------+-------+
| Field           | Type         | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+-------+
| catalog_name    | varchar(255) | NO   | PRI |         |       |
| manufacturer_id | int(11)      | NO   | PRI | 0       |       |
| description     | varchar(255) | YES  |     | NULL    |       |
+-----------------+--------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

I am trying to model a has_and_belongs_to_many relationship between Catalog and Currency. Here is my code:

#!/usr/bin/env ruby -W0

require 'rubygems'
require 'active_record'
require 'composite_primary_keys'

ActiveRecord::Base.establish_connection(...)

ActiveRecord::Base.logger = Logger.new(STDOUT)

# Models

class Currency < ActiveRecord::Base
  set_primary_key 'curr_code'
end

class CatalogsCurrency < ActiveRecord::Base
  set_primary_keys 'catalog_name', 'manufacturer_id', 'curr_code'
end

class Catalog < ActiveRecord::Base
  set_primary_keys 'catalog_name', 'manufacturer_id'

  has_and_belongs_to_many 'currencies',
    :foreign_key => ['catalog_name', 'manufacturer_id'],
    :association_foreign_key => ['curr_code']
end

# Testing

usd = Currency.find(:first, :conditions => { :curr_code => 'USD' })
eur = Currency.find(:first, :conditions => { :curr_code => 'EUR' })

cat = Catalog.find(:first) # this does not work properly

The last line of the program above does not work correctly: the catalogs_currencies table contains the following after it is executed:

mysql> select * from catalogs_currencies;
+--------------+-----------------+-----------+
| catalog_name | manufacturer_id | curr_code |
+--------------+-----------------+-----------+
|              |                 | EUR       |
|              |                 | USD       |
+--------------+-----------------+-----------+
2 rows in set (0.00 sec)

Inspecting the output of the logger, I have determined that the following SQL statement is executed:

INSERT INTO catalogs_currencies (curr_code) VALUES ('USD')

I can't figure out how to get the [catalog_name, manufacturer_id] composite primary key to be saved to the join table.

Also, I cannot figure out how to model a has_many :through relationship with composite keys. If anyone has experience with this I would appreciate any pointers.