(Ruby) the trouble of multiple primary keys (composite keys)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.