Postgres和Ruby-有更好的方法来构建我的数据库吗



我现在正在计划我的数据库。在整理ERD的过程中,我很快注意到所有表都与shops表和amazon_credentials表相关。有没有更好/更高效的设计或关系可以让我的数据库更高效、更易于使用?

这是我在Whimsical 中当前ERD的链接

还有一点需要注意,我不希望所有查询都必须遍历amazon_credentials表。例如我希望能够同时做这个Shop.find(1).packing_slips和获得shop的所有packing_slips,以及能够做这个Shop.find(1).amazon_credentials.packing_slips来获得特定市场的packing_slips

考虑到这一点,我几乎使用我的amazon_credentials表作为marketplace的替代,因为凭证的范围本质上是marketplace

这是一个好的设计还是有更好的方法?

分散在数据库中的与Shops和AmazonCredentials的重复关系可以用间接关系代替。幸运的是,Rails提供了使间接关系看起来直接的方法:has_many:through。

你不需要第一次就把它做到完美,也不需要预测每一种可能的用途。迁移可以帮助您安全地更改设计,以满足不断变化的需求。

让我们建立基本的关系。我做了一些假设,比如运输方法是附属于市场的。

  • 产品有关于自身的信息。

  • 商店有产品、营销方式(市场(、这些市场的凭证和订单。

  • 订单包括谁下了订单,从哪个商店,通过哪个市场,订购了什么产品,以什么价格订购了多少,如何发货,以及如何跟踪

  • 市场有发货方法。

我们马上遇到了一个问题:凭据。商店需要每个市场的凭证。这可以通过一个中间模型来解决:MarketplaceAccount,它将商店和市场与商店的凭证结合起来。凭据可以被制成键/值对的行。

订单需要记住订购了哪些产品、订购数量和订购金额的详细信息。这需要另一个中间模型:ProductOrder。

基本模型看起来像这样:

class Product < ApplicationRecord
belongs_to :shop
has_many :product_orders
has_many :orders, through: :product_orders
has_many :customers, through: :orders
# name
# description
# sku
end
class Shop < ApplicationRecord
has_many :products
has_many :marketplace_accounts
has_many :marketplaces, through: :marketplace_accounts

has_many :orders
has_many :customers, through: :orders
# name
# country
end
class Order < ApplicationRecord
belongs_to :customer
# Remember which MarketplaceAccount this was done with
belongs_to :marketplace_account
has_one :shop, through: :marketplace_account
has_one :marketplace, through: :marketplace_account
# How is it being shipped?
belongs_to :shipping_method

has_many :product_orders
has_many :products, through: :product_orders
# tracking number
# status
end
class ProductOrder < ApplicationRecord
belongs_to :order
belongs_to :product
# price
# quantity
end
# Holds the Shop's credentials for a Marketplace.
class MarketplaceAccount < ApplicationRecord
belongs_to :shop
belongs_to :marketplace
has_many :credentials
end
# Each row is simply a key and value.
# Consider encrypting this table.
class Credentials < ApplicationRecord
belongs_to :marketplace_account
# key
# value
end
class Marketplace < ApplicationRecord
has_many :shipping_methods
has_many :marketplace_accounts
has_many :shops, through: :marketplace_accounts
# name
end
class ShippingMethod < ApplicationRecord
belongs_to :marketplace
has_many :shops, through: :marketplace
# speed
# rate
# name
end
class Customer < ApplicationRecord
has_many :orders
has_many :product_orders, through: :orders
has_many :products, through: :product_orders
end

通过使用has_many :throughhas_one :through,我们可以避免重复,同时使间接关系看起来是直接的。商店有_个MarketplaceAccount,MarketplaceAccount属于Marketplace;使用has_many :marketplaces, through: :marketplace_accounts,商店可以直接访问其市场。shop.marketplaces

密钥/值凭据表避免了公司特定凭据表的激增。


可以充实更多的关系。一个产品可以由许多商店销售,因此我们可以有一个ShopProduct,它将一个通用产品与商店的销售方式联系起来。这可以避免重复产品信息,并允许您查看多个商店如何销售单个产品。

class Product < ApplicationRecord
has_many :shop_products
# sku
# generic name
# generic description
# manufacturer's recommended price
end
class ShopProduct < ApplicationRecord
belongs_to :shop
belongs_to :product
delegate :sku, to: :product
# shop specific name
# shop specific description
# shop price
end
class Shop
has_many :shop_products
has_many :products, through: :shop_products
end

大多数事情都与ShopProduct有关,而不是与Product有关。


您有一家使用单一货币的商店。该设计可以扩展到允许商店使用多种货币。

class Currency < ApplicationRecord
# symbol
# conversion rate
end
class ShopCurrencies < ApplicationRecord
belongs_to :shop
belongs_to :currency
end
class Shop < ApplicationRecord
has_many :shop_currencies
has_many :currencies, through: :shop_currencies
end

CCD_ 16使CCD_ 17工作。


这是基础。有了这一点,你就可以根据自己的需求进行基本布局。

最新更新