【问题标题】:Datamapper Unique Constraint and Paranoid DeletionDatamapper 唯一约束和偏执删除
【发布时间】:2014-03-28 18:52:29
【问题描述】:

我有一个带有几个字段和唯一约束的数据映射器对象。实际上,这确保了每个日期代码的每个用户 ID 只能创建一条记录。

class Form
  include Datamapper::Resource
  property :id, Serial
  property :username, String, :required => true, :index => true
  property :termcode, Integer, :required => true
  property :status, Enum[:draft, :review, :approved, :denied], :default => :draft
  ...

  property :deleted_at, ParanoidDateTime

  # only one record per user per term code
  validates_uniqueness_of :username, :scope => [:termcode], :unless => lambda{|r| r.status == :draft }

end

问题是,如果我使用偏执删除功能,唯一性约束会阻止使用相同数据创建其他记录。

例如,如果 Sue 为“2015”术语代码创建了一条记录并将其删除,她将无法创建具有相同术语代码的新记录,因为偏执功能会将原始记录保留在数据库中。

此模型的字段是出于数据完整性目的所必需的,并且在许多情况下也被索引以用于排序和更好的查询优化。

如何利用安全删除,同时允许用户输入更多记录,但告诉唯一约束忽略已删除的?

【问题讨论】:

    标签: mysql ruby datamapper unique-constraint


    【解决方案1】:

    您应该创建一个方法并在创建之前和保存之前注册它as a hook

    This is basically the same way the paranoid types operate.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-28
      • 2012-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-07
      • 2012-07-26
      相关资源
      最近更新 更多