为什么每当我登录rails应用程序时,我都会在列“id”中获得“PG :: NotNullViolation空值”违反非空约束?

问题描述 投票:0回答:1

从Heroku转储,下载和pg_restoring数据库后,每当我尝试登录我的rails应用程序时,我都会收到上述错误。

在某种程度上,似乎用户ID被视为nil(即使我可以在DB中看到id值)。

我还读到它可能是由于id是一个简单的int而不是一个序列,但是,正如你在我的架构中看到的那样:

create_table "users", id: :serial, force: :cascade do |t|
    t.string "email", default: "", null: false
    t.string "encrypted_password", default: "", null: false
    t.string "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer "sign_in_count", default: 0, null: false
    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", null: false
    t.datetime "updated_at", null: false
    t.string "name"
    t.string "last_name"
    t.string "provider"
    t.string "uid"
    t.boolean "admin", default: false, null: false
    t.boolean "affiliate_approved", default: false, null: false
    t.integer "cart_id"
    t.float "rev_share"
    t.boolean "notice", default: false, null: false
    t.string "iban"
    t.string "piva"
    t.string "invitation_token"
    t.datetime "invitation_created_at"
    t.datetime "invitation_sent_at"
    t.datetime "invitation_accepted_at"
    t.integer "invitation_limit"
    t.string "invited_by_type"
    t.integer "invited_by_id"
    t.integer "invitations_count", default: 0
    t.string "username"
    t.string "slug"
    t.integer "enrolls_count", default: 0
    t.integer "partner_id"
    t.string "country"
    t.integer "referrer"
    t.boolean "gdpr_marketing"
    t.boolean "privacy_policy"
    t.boolean "subscription", default: false
    t.date "account_created_at"
    t.boolean "profilazione"
    t.boolean "terms_and_conditions"
    t.index ["email"], name: "index_users_on_email", unique: true
    t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true
    t.index ["invitations_count"], name: "index_users_on_invitations_count"
    t.index ["invited_by_id"], name: "index_users_on_invited_by_id"
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
    t.index ["username"], name: "index_users_on_username", unique: true
  end

并且似乎已经将id用作串行。

你知道可能出了什么问题吗?这个应用程序正在生产中,有成千上万的用户没有受到这个问题的影响,这让我觉得我的本地PG设置有问题。

编辑 - 完整的错误消息

PG::NotNullViolation at /auth/google_oauth2/callback
ERROR:  null value in column "id" violates not-null constraint
DETAIL:  Failing row contains (null, 9367, 127.0.0.1, 2018-09-17 09:51:59.463125, 2018-09-17 09:51:59.463125).

编辑 - 更多发现

在user.rb(模型文件)中有一个after_update挂钩,执行以下操作:

def change_log
  UserChange.create(ip_request: current_sign_in_ip, user: self)
end

这样我们就可以跟踪用户随IP更改的所有内容(GDPR原因)。

评论完之后,一切正常,我的用户按计划登录

ruby-on-rails postgresql ruby-on-rails-5
1个回答
0
投票

必须有一个回调到另一个试图用空ID持久存在的表。检查任何相关表的回调和模式。在您的情况下,这是UserChange表。

© www.soinside.com 2019 - 2024. All rights reserved.