Ruby on rails 为什么我会得到;PG::NOTNULL违反列“中的null值”;id";违反“非空约束”;每当我登录rails应用程序时?

Ruby on rails 为什么我会得到;PG::NOTNULL违反列“中的null值”;id";违反“非空约束”;每当我登录rails应用程序时?,ruby-on-rails,postgresql,ruby-on-rails-5,Ruby On Rails,Postgresql,Ruby On Rails 5,在从Heroku转储、下载并还原DB后,每当我尝试登录rails应用程序时,我都会遇到上述错误 不知何故,似乎用户id被视为nil(即使我可以在DB中看到id值) 我还了解到,这可能是由于id是简单的int而不是serial,但是,正如您在我的模式中所看到的: create_table "users", id: :serial, force: :cascade do |t| t.string "email", default: "", null: false t.string "

在从Heroku转储、下载并还原DB后,每当我尝试登录rails应用程序时,我都会遇到上述错误

不知何故,似乎用户id被视为nil(即使我可以在DB中看到id值)

我还了解到,这可能是由于id是简单的int而不是serial,但是,正如您在我的模式中所看到的:

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 hook,其中执行以下操作:

def change_log
  UserChange.create(ip_request: current_sign_in_ip, user: self)
end
这样我们就可以跟踪用户更改的所有内容以及他们的IP(GDPR原因)


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

必须有一个对另一个表的回调,该表正试图使用空ID持久化。请检查回调和任何相关表的架构。在您的例子中,这是UserChange表

在从Heroku提取DB副本后,我遇到了相同的错误:

PG::notnull违反:错误:“id”列中的null值违反非null约束详细信息:失败的行包含…etc

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

我的问题是,我在我的开发机器上使用的Postgres版本与在Heroku上创建转储文件的版本不同。在切换到匹配版本的Postgres并重新启动DB后,一切又如预期般正常工作


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

您是否可以用尝试登录时服务器中出现的完整错误更新问题?我排除您并非试图创建新用途,而是先强制id为零。例如,身份验证代码可能有错误。@Pavan justupdated@TomDunning-我唯一感到奇怪的是,这只发生在我的本地机器上。团队中的其他开发人员能够注册/登录,没有PG错误whatsoever@AndreaRocca你确定里面没有空id的数据吗?如果是我,我会尝试删除数据库,然后创建它,然后再次查看数据。如果这不能解决它(而且你的种子肯定有ID),那么它必须是一个新的记录,它试图创建