# User.find\_by(id: 123) 失败：无登录，无管理权限

**URL:** <https://meta.discourse.org/t/user-find-by-id-123-fails-no-login-no-adminstration/117176>\
**Category:** Self-hosting\
**Created:** [2019年五月8日 14:01 UTC](https://meta.discourse.org/t/user-find-by-id-123-fails-no-login-no-adminstration/117176 "2019-05-08T14:01:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Blizzz](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@Blizzz](https://meta.discourse.org/u/Blizzz)\
**Post date:** [2019年五月8日 14:01 UTC](https://meta.discourse.org/t/user-find-by-id-123-fails-no-login-no-adminstration/117176/1 "2019-05-08T14:01:32Z")

</div>

We have some users where the profile page is visible, but neither login works for them nor can I administer them. It always results in a 404 page.

I went to the rails console and tried to look up the user by id and username:

```plaintext
[2] pry(main)> User.find_by(id: 2)
=> nil
[3] pry(main)> User.find_by(username_lower: "juanettedoe")
=> #<User:0x00005622930e5770
 id: 2,
 username: "juanettedoe",
 …

```

So, looking up via username works and it reveals the correct user id. But, looking up by user id does not result in the dataset. What can I do about it?

---

<div class="post-metadata">

**Author:** ![supermathie](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/supermathie/32/507518_2.png) [@supermathie](https://meta.discourse.org/u/supermathie)\
**Post date:** [2019年五月8日 14:13 UTC](https://meta.discourse.org/t/user-find-by-id-123-fails-no-login-no-adminstration/117176/2 "2019-05-08T14:13:08Z")

</div>

This makes me think that your postgres indices are corrupt.

I would recommend first following the steps listed on [Corruption - PostgreSQL wiki](https://wiki.postgresql.org/wiki/Corruption) to preserve your data.

After you’ve made a copy if you run the postgres CLI and try that same underlying query you should results like:

```plaintext
discourse_development=# SELECT "users"."id", "users"."username" FROM "users" WHERE "users"."id" = 2 LIMIT 1;
 id | username 
----+-------------
  2 | juanettedoe
(1 row)

discourse_development=# SELECT "users"."id", "users"."username" FROM "users" WHERE "users"."username_lower" = 'juanettedoe' LIMIT 1;
 id | username 
----+-------------
  2 | juanettedoe
(1 row)

```

---

<div class="post-metadata">

**Author:** ![Blizzz](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@Blizzz](https://meta.discourse.org/u/Blizzz)\
**Post date:** [2019年五月8日 14:21 UTC](https://meta.discourse.org/t/user-find-by-id-123-fails-no-login-no-adminstration/117176/3 "2019-05-08T14:21:15Z")

</div>

Thanks, I’ll check this out!

---

<div class="post-metadata">

**Author:** ![Blizzz](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@Blizzz](https://meta.discourse.org/u/Blizzz)\
**Post date:** [2019年五月17日 22:39 UTC](https://meta.discourse.org/t/user-find-by-id-123-fails-no-login-no-adminstration/117176/4 "2019-05-17T22:39:24Z")

</div>

Indeed it was an index corruption issue. I think i sorted it out – thanks!

---

<div class="post-metadata">

**Author:** ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)\
**Post date:** [2019年六月16日 22:39 UTC](https://meta.discourse.org/t/user-find-by-id-123-fails-no-login-no-adminstration/117176/5 "2019-06-16T22:39:27Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
