Recently, rake was used for database migration. Ruby's activerecord only supports single-primary-key tables, which is very troublesome for the migration of compound-key tables.
Assume that table A1 has a Sid primary key, and its storage is as follows:
Class init <activerecord: Migration <br/> Class A1 <activerecord: Base <br/> set_table_name "A1" <br/> set_primary_key: sid <br/> end </P> <p> def self. up <br/> A1 = a1.new <br/> a1.pid = '000000' <br/> a1.sid = '000000' <br/> a1.name = 'aaa' <br/> a1.save! <Br/> end
However, when the PID is also the primary key, it will be difficult to save and there will be a primary key constraint problem. g later found that there is a project in ruby, composite primary keys, specifically used to supplement activerecords, d. You can use it on gem.
If sid and PID are compound keys, save the statement as long as it is changed to red.
Require 'composite _ primary_keys '# introduce this package <br/> class init <activerecord: Migration <br/> Class A1 <activerecord :: base <br/> set_table_name "A1" <br/> set_primary_keys: Sid,: pid # key changes to keys <br/> end </P> <p> def self. up <br/> A1 = a1.new <br/> a1.pid = '000000' <br/> a1.sid = '000000' <br/> a1.name = 'aaa' <br/> a1.save! <Br/> end <br/>
Http://rubyforge.org/projects/compositekeys