Datamapper - 在belongs_to属性上创建唯一索引

时间:2013-08-22 22:09:09

标签: ruby ruby-datamapper

我正在使用连接到SQLite后端的DataMapper。我需要在我的四个belongs_to列中创建一个Unique索引。这是表格。

class Score 
include DataMapper::Resource

property :id, Serial
property :score, Integer

belongs_to :pageant
belongs_to :candidate
belongs_to :category
belongs_to :judge

#What we want is a  UNIQUE INDEX on the four properties!

end

我做过的事情:

  1. 四者的唯一索引,例如:unique_index => :single_score。仅当您已经包含属性时才有效。
  2. validates_uniqueness_of,我认为范围仅适用于2列唯一索引。
  3. 我目前的解决方案,就是创建一个虚拟字段“dont_mind_me”,这样我就可以放下:unique_index => single_score在其中,一切正常。这是可以的吗?
  4. 使用原始SQL创建索引,SQLite支持四个字段中的唯一索引。
  5. 这个问题基本上有两个部分:

    1. 我的解决方案可以,或者我应该找到另一个?即使使用原始SQL
    2. ,我也很难处理看似微不足道的事情
    3. 如何在DataMapper中创建“after_create_table”挂钩?文档中的钩子只讲述CRUD后的数据。

0 个答案:

没有答案