Ruby on rails development from scratch ()-ActiveRecord BASICS (primary key and ID)

Source: Internet
Author: User
You may have noticed that in our previous code, the database definition uses an integer field id as the primary key, which is a convention of Active Record.

Maybe you have to ask, why don't we use order numbers or a meaningful column as the primary key? An important reason for using id as the primary key is that if you use a primary key with an internal format, the rules may change over time. For example, if you use an ISBN number to make a primary key for the book table, after all, the ISBN number is unique, but it is possible that after a book is written, the publishing industry in the United States has developed and a number has been appended to all ISBN numbers.

If we use ISBN as the primary key of the book table, We need to update the records of all book tables to reflect this change. There is also a problem that other tables reference the primary key of the book table, we need to update all references, which also involves deleting Foreign keys. All of these are very painful.

If meaningful values are used as the primary key, we will be affected by external business rules. If id is used, we can completely control it, and if something like ISBN changes, the database structure will not be affected.

If you start from a new database structure, you may follow the conventions to use id as the primary key for all tables. However, when you start using a database that already exists, active Record provides a simple method for you to specify a primary key for the table. For example:

ClassBadBook <ActiveRecord: Base

Set_primary_key"Isbn"

End

Generally, Active Record generates a primary key value for the newly created Record-an auto-increment integer. In any case, when you name the primary key of the override table, you need to create a unique primary key value for the new record. It may be surprising that you still need to set an id attribute to complete this task, because Active Record is concerned that the primary key setting always uses the attribute named id, the set_primary_key declaration only sets the column name used. In the following example, we use ISBN as the primary key.

Book = BadBook. new

Book. id ="0-12345-6789"

Book. title ="My Great American Novel"

Book. save

#...

Book = BadBook. find ("0-12345-6789")

Puts book. title# => "My Great American Novel"

P book. attributes#=>{ "Isbn" => "0-12345-6789 ",

"Title"=>"My Great American Novel"}

That is to say, when setting the primary key, the id attribute is used. Otherwise, the real column name is used.

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.