0

因此,我设置了一个登台并开发分支,现在在本地注册新用户时出现此错误:

    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 片段

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

在解决这个问题之前,我无法将任何新代码投入生产,这很烦人:/

4

2 回答 2

2

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

于 2013-08-13T08:02:51.847 回答
2

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

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

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

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

于 2013-08-13T08:03:36.627 回答