不是的.
序列化的列在现代应用程序中没有立足之地.它们是在本机JSON/JSONB列广泛使用之前的一种过度使用的肮脏黑客,只有一些缺点.该规则的唯一例外是使用应用程序端加密时.
JSON/JSONB列可以用于有限数量的任务,在这些任务中,数据不能由固定的模式定义,或者如果您只是存储原始的JSON响应--但它不应该是您定义模式的方式,因为you're just shooting yourself in the foot.是用于特殊作业(job)的特殊工具.
更好的替代方法是实际使用良好的关系数据库设计,并将销售时的价格和其他所有信息存储在单独的表中:
class Order < ApplicationRecord
has_many :line_items
end
# rails g model line_item order:belongs_to product:belongs_to units:decimal unit_price:decimal subtotal:decimal
# The line item model is responsible for each item of an order
# and records the price at the time of order and any discounts applied to that line
class LineItem < ApplicationRecord
belongs_to :order
belongs_to :product
end
class Product < ApplicationRecord
has_many :line_items
end
序列化的列无论如何都不是一成不变的--它实际上更容易被反规范化和损坏,因为没有数据库端约束来确保它的正确性.
实际上,通过使用触发器,许多数据库中的表都是不可变的.
优点:
- 不违反1NF.
- 要使用的规范化固定数据模式-约束可确保数据在数据库级别上的有效性.
- 连接是一种非常强大的工具,并不像您想象的那样昂贵.
- 如果需要,您可以在应用程序外部实际访问和理解数据.
- DECIMAL数据类型.JSON/JSONB只有一个使用IEEE 754浮点的数字类型.
- 您拥有了一个实际的模型和关联,而不必处理原始的散列.
- 您可以在正常的查询中查询数据.
- 您可以在数据库级别生成聚合,并使用实例化视图等工具.