是否ActiveRecord的键指定给使用命名约定和QUOT每个表; ID",如果是的话,为什么呢? [英] Does ActiveRecord assign a key to every table using the naming convention "ID", and if so, why?

查看:216
本文介绍了是否ActiveRecord的键指定给使用命名约定和QUOT每个表; ID",如果是的话,为什么呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的理解是,Actice记录是基于Martin Fowler的在他的著作企业应用架构模式(艾迪生韦斯利,2002)的模式描述的对象关系映射(ORM)模式;其中规定一个数据库记录,并且重新presents它在一个面向对象的程序设计(OOP)对象之间存在一个一到一的映射关系。当Rails的创始人大卫Heinemeier力求实现一个ORM他Rails框架,他基于它福勒的模式。

My understanding is that Actice Record is based on a object-relational mapping (ORM) pattern described by Martin Fowler in his book Pattern of Enterprise Application Architecture (Addison-Wesley, 2002); which states a one-to-one mapping relationship exists between a database record and the object that represents it in an object-oriented program (OOP). When Rails creator David Heinemeier sought to implement an ORM for his Rails framework, he based it on Fowler's pattern.

这里的问题,并ActiveRecord的分配使用的命名约定ID代理主键,每个表,如果是这样,为什么呢?原因我想问的是,它似乎它会更合理使用的命名约定tablename_ID分配AA代理主键;因为事实上它出现在创建外键时ActiveRecord的一样。此外,是否有可能覆盖默认的配置,并使用命名约定tablename_ID指定代理主键;原因是,特别是在主键的情况下,这似乎是不使用共享名是一个好主意,由于告诉两列的ID是不可能的,如果简单地寻找在柱namesing之间的差:ID,ID编号。作为其中这似乎会有一个问题,如果我的数据导出到单个表从两个表使用情况实例,将有两列与ID名称;当我导入与更新的文件,这样看来,有没有办法在默认情况下映射ID列。

Here's the problem, does ActiveRecord assign a surrogate primary key to every table using the naming convention "ID", and if so, why? Reason I ask is that it appears it'd make more sense to assign a a surrogate primary key using the naming convention "tablename_ID"; as in fact it appears ActiveRecord does when creating foreign keys. Further, is it possible to override the default config, and assign a surrogate primary key using the naming convention "tablename_ID"; reason being that especially in the case of primary keys, it appears to be a good idea not to use a shared name, since telling the difference between two columns ID's is not possible if simply looking at the column namesing: ID, ID, ID. As a use case example of where this appears there would be a problem, if I export data to a single table from two tables, there will be two columns with the ID name; when I import that document with updates, it would appear that there's no way to map the ID columns by default.

推荐答案

它已经很明显地看到ID是什么,因为它是在一定的表格。你不必 table_name.table_name_id ,但 table_name.id 。我喜欢外键和主键这样的区别,但不可否认,这是见仁见智的问题。

It's already obvious to see what the ID is, because it is in a certain table. You don't have table_name.table_name_id but table_name.id. I like the distinction between foreign keys and primary keys this way, but admittedly, it's a matter of opinion.

除非你有一个真的不可使用任何其它原因,你应该坚持的惯例。这就是约定的。 ActiveRecord的让您选择去改变它,像这样:

Unless you have a really good reason to use anything other, you should stick to the convention. That's what conventions are for. ActiveRecord gives you the option to change it, like so:

ActiveRecord::Base.primary_key_prefix_type = :table_name_with_underscore

此设置在全球范围内产生主键的方式。 products.id 变成 products.product_id

This sets the way primary keys are generated globally. products.id becomes products.product_id.

这篇关于是否ActiveRecord的键指定给使用命名约定和QUOT每个表; ID",如果是的话,为什么呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆