代码之家  ›  专栏  ›  技术社区  ›  John F. Miller

数据映射器多字段唯一索引

  •  8
  • John F. Miller  · 技术社区  · 15 年前

    在数据映射器中,如何指定两个字段的组合必须是唯一的。例如,类别在域中必须具有唯一的名称:

    class Category
      include DataMapper.resource
      property :name, String, :index=>true #must be unique for a given domain
    
      belongs_to :domain
    end
    
    3 回复  |  直到 13 年前
        1
  •  1
  •   Jonas Elfström    15 年前

    是否尝试将这两个属性都定义为键?不确定我是否尝试过,但这样它们就应该成为复合键。

    property :name, String, :key => true    
    property :category, Integer, :key => true
    
        2
  •  16
  •   joschi    15 年前

    必须为这两个属性创建唯一索引:

    class Category
      include DataMapper::Resource
    
      property :name, String, :unique_index => :u
      property :domain_id, Integer, :unique_index => :u
    
      belongs_to :domain
    end
    
        3
  •  2
  •   cobbr2    13 年前

    实际上,joschi的答案是正确的:使用命名的:unique-index值确实创建了一个多列索引;阅读这些散列火箭的右边是很重要的(例如,如果它只是 true 你说得对)。

    推荐文章