为什么每当我登录我的 rails 应用程序时"PG::NotNullViolation null value in column "我都会得到 ID " violates not-null const



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

不知何故,用户 id 似乎被视为 nil(即使我可以在数据库中看到 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 原因(。

注释掉后,一切正常,我的用户按计划登录

必须有一个回调到另一个试图使用 null ID 保留的表。 检查任何相关表的回调和架构。在您的情况下,这是用户更改表。

从 Heroku 拉取数据库副本后,我遇到了同样的错误:

PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains ...etc

每次我试图创造新记录时,都会发生这种情况。 我猜当你登录时,你的应用程序正在某处创建一个新记录(可能是你正在做的一些登录跟踪/日志记录?(触发错误。 如果您无法通过 Web 浏览器访问该应用程序,您可以尝试打开 Rails 控制台,看看如果您尝试直接创建新记录会发生什么。 如果仍然出现错误,则问题实际上不在于您的登录,而在于您的数据库。

对我来说,问题是我在开发机器上使用的 Postgres 版本与创建转储文件的 Heroku 版本不同。 切换到 Postgres 的匹配版本并重新拉取数据库后,一切再次按预期工作。

如果您在 Mac 上使用 Postgres,则在添加新的 Postgres 服务器时,您可以选择它应该使用的 Postgres 版本。 您希望使开发和生产环境尽可能相似,因此您肯定希望确保数据库版本匹配。

最新更新