首页 > 解决方案 > Rails 将值保存到数据库超出限制

问题描述

我在 Rails 6 中有这个模式

create_table "fields", force: :cascade do |t|
t.string "title", limit: 100
t.text "description"
t.string "slug", limit: 20
t.integer "requests"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["slug"], name: "index_fields_on_slug", unique: true
t.index ["title"], name: "index_fields_on_title", unique: true

在rails控制台内部:

field = Field.new
field.slug = 'a' *25
field.save

并且该条目已成功保存在字段表中。为什么?我的 :slug 限制为 20。如果没有在模型中验证,那不应该失败吗?

标签: ruby-on-railsdatabasemigration

解决方案


推荐阅读