【问题标题】:shema_plus with rails migration: make combined columns uniqueshema_plus 与 rails 迁移:使组合列独一无二
【发布时间】:2014-10-19 01:26:02
【问题描述】:

我在 rails 中使用 shema_plus gem,我想这样做,所以如果两列的组合相同,我无法将数据复制到我的数据库中。

这不应该工作吗?:

t.string :name, null: false, index: { with: :deleted_at, unique: true }

':deleted_at' 绝对是我迁移中的一个字段。

但这允许我在 mysql 端输入两次相同的名称。

另外,这里是mysql给我的一些信息:

Index: index_plans_on_name_and_deleted_at
Definition:

Type    BTREE
Unique  Yes
Columns name
    deleted_at

编辑_____________________________________________________

看起来我需要为 deleted_at 设置一个默认值,例如:

t.datetime :deleted_at, default: '2000-01-01' 

【问题讨论】:

    标签: ruby-on-rails schema


    【解决方案1】:

    不确定 gem,但您可以直接在 ActiveRecord 中使用:

    validates :name, :uniqueness => {:scope => :deleted_at}

    假设您希望 namedeleted_at 的组合是唯一的。

    【讨论】:

    • 是的,谢谢!!...只是试图在两端锁定它。看起来我必须为 deleted_at 设置一个默认值,例如:t.datetime :deleted_at, default: '2000-01-01'
    猜你喜欢
    • 2013-06-04
    • 1970-01-01
    • 2011-09-27
    • 2011-02-05
    • 1970-01-01
    • 2014-09-08
    • 2018-11-24
    • 1970-01-01
    相关资源
    最近更新 更多