PG ::错误:错误:列“uid”中的空值违反了非空约束

时间:2013-08-13 07:22:55

标签: ruby-on-rails

所以,我设置了一个分段,并开发分支,现在我在本地注册一个新用户时遇到了这个错误:

    PG::Error: ERROR: null value in column "uid" violates
 not-null constraint DETAIL: Failing row contains
 (7, test@gmail.com, $2a$10$WfiHKzYq4ovZtZoVAB.fRertuOSWezZYBw.RxkSqfBK89WbzcYxDK, 
null, null, null, 0, null, null, null, null, 
2013-08-13 07:20:13.484617, 2013-08-13 07:20:13.484617, 
null, null, null, null). : INSERT INTO "users" 
("created_at", "email", "encrypted_password", "updated_at") 
VALUES ($1, $2, $3, $4) RETURNING "uid"

schema.rb snippit

create_table "users", id: false, force: true do |t|
    t.integer  "id",                                  null: false
    t.string   "email",                  default: ""
    t.string   "encrypted_password",     default: ""
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.boolean  "admin"
    t.string   "provider"
    t.string   "uid",                                 null: false
    t.string   "username"
  end

  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

在解决之前我无法将任何新代码推送到生产环境,这很烦人:/

2 个答案:

答案 0 :(得分:2)

问题是您在数据库中创建新用户的代码没有为该行定义uid值。您的数据库已限制该字段以禁止将行uid作为空行。因此,您应该从数据库中删除此约束,或者更改代码以通过uid

答案 1 :(得分:2)

您的uid不可为空,但您没有给它任何值。在注册用户时,让您的应用程序生成一个。

Ruby有一个名为Secure Random的模块,可用于生成uid。它的用法也很简单。像,

require 'securerandom'
uid = SecureRandom.hex(10)

但是,您可以使用factory girl初始化对象。但是为了测试目的。

相关问题