User.find_by(id: 123) fails: no login, no adminstration

AI-generated summary

A user, Blizzz, reports that User.find_by(id: 123) fails with a 404 error, even though the user’s profile is visible and lookup by username works. In the Rails console, the user’s ID is found via username_lower but not directly by ID. supermathie suspects PostgreSQL index corruption and suggests verifying the issue using direct SQL queries. Blizzz confirms it was indeed an index corruption issue and resolves it.

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:

[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?

This makes me think that your postgres indices are corrupt.

I would recommend first following the steps listed on Corruption - PostgreSQL wiki 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:

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)

Thanks, I’ll check this out!

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