【发布时间】:2015-03-31 17:28:30
【问题描述】:
假设我有一个销售不同品牌和规格的笔记本电脑的网站。规格包括 CPU 功率、内存、硬盘空间,甚至保险、服务和配件等。
它们都应该可以通过完整搜索文本和分面/过滤进行搜索,例如:http://www.pricegrabber.com/computers/laptop/p-13/
数据库是postgresql。
无论是否愚蠢,我的计划是为每个规范本身设置一个表格。所以我会为cpus 创建一个表,其中有所有不同的 CPU 选项,例如“Intel 4.33 Ghz 四核”或其他任何东西,并对hard_disks、memories、video_cards、等等。
因此,最后,主表看起来像这样:
class CreateLaptops < ActiveRecord::Migration
def change
create_table :laptops do |t|
t.references :brands_id
t.references :models_id
t.references :cpus_id
t.references :memories_id
t.references :video_cards_id
t.references :displays_id
t.references :batteries_id
t.references :accessories_id
t.references :insurances_id
# and the list goes on ...
t.timestamps null: false
end
end
end
这是愚蠢的设计吗?我知道这可能是基于意见,但我会喜欢任何指针,比如如果我客观地做完全错误的事情。我有点计划更深入,即引用一些东西,而后者又引用了一些东西。例如,cpus 可以拆分为 intel_cpus 和 amd_cpus,但现在我觉得我走得太远了。
最重要的考虑是使其易于从外部搜索,然后使其易于维护和组织,而性能则排在最后,因为数据库中根本不会有很多项目。
期待听到您的想法!
【问题讨论】:
-
你为什么不使用“文章编号”之类的东西作为主键?有了这样一个数字,您就可以唯一确定每个产品。
-
@SklogW 我想这是自动分配的。除非我误解了你,否则这不是问题的关键。
-
看看here
标签: ruby-on-rails postgresql database-design