複合キー

Ruby on Rails does not support composite primary keys. This free software is an extension to the database layer of Rails 〓 ActiveRecords 〓 to support composite primary keys as transparently as possible.

RoRでどこまで複合キーを使えるのか、検索してみたらそのまんまの名前のライブラリがあった。ただしプラグインではなくgemsでインストールする。

使い方は、set_primary_keyではなくset_primary_keysでプライマリキーを複数指定できる。
同様にbelongs_toやhas_manyのforeign_keyではなくforeign_keysも使える・・・はずなんだけど、foreign_keyしかだめかも。

まあ、これを使っていくにはちょっと色々注意する必要がありそうなんだけど、Railsに欠かせないのがマイグレーション。これをcomposite_primary_keys用に拡張するプラグインを発見。

It was created for maintaining legacy database schemas that use composite primary keys, but not only do we encourage their use, we actually made more work for ourselves on Chow and Chowhound so we could use them.

超便利ー、やっぱり皆考えるのは一緒ね。
書き方はcreate_tableのオプション:primary_keyに配列でカラムを複数していすればOK。


しかし、composite_primary_keysをGoogleで検索した件数がウェブ全体で169件てどういうこと?他に何かそういうものがあるのかな?それとも何か秘密があるのか。http://wiki.rubyonrails.com/にそういうの載ってそうだけど、今夜はService Temporarily Unavailableで見れないよ。