问题描述
假设以下数据模式:
Usage
======
client_id
resource
type
amount
Billing
======
client_id
usage_resource
usage_type
rate
在这个例子中,假设我有多个资源,每个资源可以在很多方法。例如,一个资源是小部件
。 小部件
可以是 foo
ed,它们可以是 bar
ed 。 gizmo
也可以是 foo
ed和 bar
ed。这些使用类型以不同的费率收费,对不同的客户来说可能甚至不同。使用情况(资源)的每次出现记录在使用情况表中。每个计费率(用于客户端,资源和类型组合)存储在计费表中。
In this example, suppose I have multiple resources, each of which can be used in many ways. For example, one resource is a widget
. Widgets
can be foo
ed and they can be bar
ed. Gizmo
s can also be foo
ed and bar
ed. These usage types are billed at different rates, possibly even different rates for different clients. Each occurence of a usage (of a resource) is recorded in the Usage table. Each billing rate (for client, resource, and type combination) is stored in the billing table.
(顺便说一句,如果这个数据模式不是正确的方法来解决这个问题,请提出建议。)
使用Ruby on Rails和ActiveRecord可以创建一个 has_many
从Billings到Usages的关系,以便我可以获得给定计费费用的使用情况列表?有没有一个 has_many,...通过
的语法,我不知道?
Is it possible, using Ruby on Rails and ActiveRecord, to create a has_many
relationship from Billings to Usages so that I can get a list of usage instances for a given billing rate? Is there a syntax of the has_many, :through
that I don't know?
再次,我可以从错误的角度接近这个问题,所以如果你能想到一个更好的方法,请说出来!
Once again, I may be approaching this problem from the wrong angle, so if you can think of a better way, please speak up!
推荐答案
有显然是sourceforge的一个项目来扩展Rails的ActiveRecord,支持。我没有使用这个扩展名,但它可能会帮助你。这也是rubyforge的宝石。
There is apparently an project at sourceforge to extend Rails' ActiveRecord with support for Composite Primary Keys. I haven't used this extension, but it might help you. It's also a gem at rubyforge.
从2.0版开始,纯Ruby on Rails不支持复合主键(参见)。每个表必须有一个名为 id
的单列自动递增键。
Plain Ruby on Rails, as of version 2.0, does not support compound primary keys (cf. HowToUseLegacySchemas). Every table must have a single-column, auto-increment key named "id
".
我所看到的解释是:如果要使用旧数据库,您只需要复合主键。这当然是一个荒谬的数据建模视图。
The explanation I have seen is: "You only need compound primary keys if you want to use a legacy database." This is of course a ridiculously ignorant view of data modeling.
我看到的解决方案是:
- Usage.client_id - > Client.id
- Usage.type_id - > Usagetype.id
- Usage.resource_id - >资源.id
- Billing.usage_id - > Usage.id
- Billing.client_id - > Client.id
- Billing.type_id - > Usagetype.id
- Billing.resource_id - > Resource.id
- Usage.client_id -> Client.id
- Usage.type_id -> Usagetype.id
- Usage.resource_id -> Resource.id
- Billing.usage_id -> Usage.id
- Billing.client_id -> Client.id
- Billing.type_id -> Usagetype.id
- Billing.resource_id -> Resource.id
Billing
中明显冗余的外键试图强制执行部分引用完整性。但是它并不完全相同 - 它不会阻止您在 Billing
中创建行,引用中的一行使用
使用错误的客户端/资源/ usagetype组合,不符合帐单表中引用行中的那些。
The apparently redundant foreign keys in Billing
attempt to enforce partial referential integrity. But it doesn't quite get there -- it does not prevent you from creating rows in Billing
that reference a row in Usage
with the wrong client/resource/usagetype combination, not matching those in the referencing row in the Billing table.
编辑: @Yarik : 你是对的。 使用
可以引用结算
。
edit: @Yarik: yes, you're right. It makes more sense for Usage
to reference Billing
.
- Usage.billing_id - > Billing.id
嗯。我制作了一个ER图,但我无法将其插入图像。
Hmm. I made an ER diagram but I'm having trouble inserting it as an image.
这篇关于是否可以在钢轨上安装复合外键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!