Does ActiveRecord activate a key for each table using the "ID" naming convention, and if so, why?

I understand that an Actice record is based on an object-relational mapping (ORM) pattern described by Martin Fowler in his book "Structure of Enterprise Application Architecture" (Addison-Wesley, 2002); which states that there is a one-to-one mapping relationship between the database record and the object representing it in the object-oriented program (OOP). When Rails creator David Heinmeyer tried to implement ORM for his Rails framework, he based it on the Fowler pattern.

In this case, ActiveRecord assigns a surrogate primary key for each table using the "ID" naming convention, and if so, why? The reason I'm asking is that it seems to make sense to assign a surrogate primary key using the naming convention "tablename_ID"; since it actually appears when creating ActiveRecord foreign keys. Also, is it possible to override the default configuration and assign a surrogate primary key using the naming convention "tablename_ID"; the reason is that especially in the case of primary keys, it seems a good idea not to use a shared name, since it is impossible to talk about the difference between the two column identifiers simply by looking at the column names: ID, ID, ID. As an example of using where this appears, there would be a problem if I export data to one table from two tables, there will be two columns with an identification name; when I import this document with updates, it would seem that there is no way to map the default identifier columns.

+1
ruby-on-rails activerecord naming-conventions orm
source share
1 answer

It is already obvious what an identifier is, because it is in a specific table. You do not have table_name.table_name_id , but table_name.id . I like the distinction between foreign keys and primary keys, but admittedly this is a matter of opinion.

If you have a really good reason to use something else, you must adhere to the agreement. For what conventions. ActiveRecord gives you the ability to change it, for example:

 ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore 

Indicates how primary keys are generated globally. products.id becomes products.product_id .

+3
source share

All Articles