Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ror
ruby

What are schema conventions in ROR?

Behzad Ahmad

Database schemas in Active Record employ certain naming conventions for the table columns depending on the purpose that the table serves.

The two main types of keys used by tables are:

  • Primary Keys
  • Foreign Keys

Primary key

If you have some column in your table named id, then the default active record will select that column as the primary key. When you create tables with Active Record Migration, this column is automatically added to the database table.

Foreign key

These are the fields in other tables that refer to the primary key of another table with which the other table is associated with. Its naming convention follows the name of the original table followed by an underscore and then id.

If the name of the table has more than more one word, it is converted into a single word name.

widget

Additional functions

In order to add additional features, there are optional columns :

  • created_at: When we create the first record, it automatically sets the date and time for that record.

  • updated_at: When we modify or create the record, it updates the date and time.

  • lock_version: Prevents the data from overriding by assuming that a database transaction conflict rarely happens. This can be done by adding an optimistic lock.

  • type: Specifies the type of model.

  • (association_name)_type: Stores the type for polymorphic associations.

RELATED TAGS

ror
ruby

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring