Rails,列中的空值违反了非空约束

时间:2016-10-09 23:26:24

标签: ruby-on-rails

在制作中,我有一个模型类别。当我尝试在网站上创建一个类别时,我收到以下错误

ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR:  null value in column "name" violates not-null constraint

迁移看起来像这样

class CreateCategories < ActiveRecord::Migration
  def up
    create_table :categories do |t|
      t.references :supercategory,     index: true
      t.string :name

      t.timestamps null: false
    end
    Category.create_translation_table! name: :string
  end
  ....
end

架构

  create_table "categories", force: :cascade do |t|
    ...
    t.string   "name"
    t.datetime "created_at",          null: false
    t.datetime "updated_at",          null: false
    t.string   "slug"
    t.index ["restaurant_id"], name: "index_categories_on_restaurant_id", using: :btree
    t.index ["slug"], name: "index_categories_on_slug", unique: true, using: :btree
    t.index ["supercategory_id"], name: "index_categories_on_supercategory_id", using: :btree
  end

任何可能导致错误的想法?谢谢! 如果您需要更多代码,请询问。

EDIT 发送的参数

  

参数:{“utf8”=&gt;“✓”,   “authenticity_token”=&gt; “中no3uGNSYkgClG + y6y9Y =”,   “category”=&gt; {“name”=&gt;“Coffee”,“age_restriction”=&gt;“0”,   “available_all_day”=&gt;“0”,“supercategory_id”=&gt;“”,   “化身”=&GT;#,   @ original_filename =“coffee.JPG”,@ content_type =“image / jpeg”,   @ headers =“Content-Disposition:form-data; name = \”category [avatar] \“;   filename = \“coffee.JPG \”\ r \ nConContent-Type:image / jpeg \ r \ n“&gt;},   “commit”=&gt;“创建类别”,“remotipart_submitted”=&gt;“true”,   “X-Requested-With”=&gt;“IFrame”,“X-Http-Accept”=&gt;“text / javascript,   application / javascript,application / ecmascript,   application / x-ecmascript, / ; Q = 0.01" ,   “restaurant_id”=&gt; “中乞力马扎罗咖啡”}

EDIT 我试图从控制台创建一个类别,我发现该模型没有名称列。好像它还没有正确迁移。我再次删除,创建和迁移数据库,但仍未显示该列。有什么想法来自哪里?

1 个答案:

答案 0 :(得分:0)

很难想象如果没有PostgreSQL表将该列设置为非null,您将如何获得该错误,因此我怀疑您没有使用您认为正在使用的数据库。

您可以使用以下命令检查活动记录认为哪些列设置为非空:

Category.columns.reject{|c| c.null}.map(&:name)
相关问题