22

Developing an API-only Rails app to consume data from an API, filter out a number of the fields, and then broadcast fields we want public.

My app was returning data normally, but I realized that on the source API level, we changed the data type for one of the fields of data we're exposing. To accommodate, I committed my work, checked out a new branch, ran a migrations to change a data field's data type, decided I wasn't happy with how it worked, rolled back the migration, committed, and checkout out the master branch. The app is returning data as normal. However, now when I test my model's records in Rails Console using finder methods like .first, .last, etc., I get the error below. They were working fine before.

I've looked around and haven't seen any threads on this particular error (they all seem to deal with finding individual records for the show page) - although this, and this thread (by extension) seem closest. And checking the users on the db I'm using shows that the user ID ("501") in the error is not present.

What did I do and what do I need to do so that I can call finder methods again? Do I need to create that user referenced in the error in my db? (and why is it not the default user that I always use to connect to my db? Where did that user ID in the error come from? Does it even matter?)

ps - in case it's relevant, I used rake db:rollback to rollback my migration as per this thread.

Thanks in advance.

Rails Console

.2.1 :001 > KoboApi.first
ActiveRecord::NoDatabaseError: local user with ID 501 does not exist

    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:242:in `initialize'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-postgis-adapter-3.1.4/lib/active_record/connection_adapters/postgis_adapter.rb:51:in `initialize'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-postgis-adapter-3.1.4/lib/active_record/connection_adapters/postgis/create_connection.rb:37:in `new'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-postgis-adapter-3.1.4/lib/active_record/connection_adapters/postgis/create_connection.rb:37:in `postgis_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:438:in `new_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:448:in `checkout_new_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:422:in `acquire_connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:349:in `block in checkout'
    from /Users/toby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:348:in `checkout'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:263:in `block in connection'
    from /Users/toby/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/monitor.rb:211:in `mon_synchronize'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:262:in `connection'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:571:in `retrieve_connection'
... 13 levels...
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/commands/console.rb:9:in `start'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:68:in `console'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/railties-4.2.5.1/lib/rails/commands.rb:17:in `<top (required)>'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `block in require'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:274:in `require'
    from /Users/toby/code/projects/koboApi-broker/bin/rails:9:in `<top (required)>'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `block in load'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:240:in `load_dependency'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activesupport-4.2.5.1/lib/active_support/dependencies.rb:268:in `load'
    from /Users/toby/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
    from /Users/toby/.rvm/rubies/ruby-2.2.1/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'

I'm definitely getting my records - they're coming into the database:

Rails dbconsole

koboApi_development=# select * from kobo_apis limit 1;
  id  | lemurs_quantity | month_and_year | _geolocation | lemur_category | location_admin1 | location_admin2 | record_id | sighting_month | sighting_year
------+-----------------+----------------+--------------+----------------+-----------------+-----------------+-----------+----------------+---------------
 1365 |               1 |                |              | I_dont_remembe | antsiranana     | diana           |   1234567 | no_response    | 2013
(1 row)

koboApi_development=# \du
                             List of roles
 Role name |                   Attributes                   | Member of
-----------+------------------------------------------------+-----------
 [user]    | Superuser, Create role, Create DB, Replication | {}

from my schema

ActiveRecord::Schema.define(version: 20160705203507) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"
  enable_extension "postgis"

  create_table "kobo_apis", force: :cascade do |t|
    t.integer "lemurs_quantity"
    t.date    "month_and_year"
    t.text    "_geolocation"
    t.text    "lemur_category"
    t.string  "location_admin1"
    t.string  "location_admin2"
    t.integer "record_id"
    t.string  "sighting_month"
    t.string  "sighting_year"
  end

  create_table "my_spatial_table", force: :cascade do |t|
    t.geography "polygon_data", limit: {:srid=>4326, :type=>"polygon", :geographic=>true}
  end

end

My database.yml

development:
  adapter: postgis
  encoding: unicode
  postgis_extension: postgis      # default is postgis
  postgis_schema: public          # default is public
  schema_search_path: public,postgis
  database: koboApi_development
  pool: 5

test:
  adapter: postgresql
  encoding: unicode
  database: koboApi_test
  pool: 5

production:
  adapter: postgresql
  encoding: unicode
  database: koboApi_production
  pool: 5
Community
  • 1
  • 1
Mugshep
  • 788
  • 1
  • 9
  • 18

3 Answers3

80

It seems to be Spring's problem, not the database if you're getting this error:

/Users/yad/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.6/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect': local user with ID 501 does not exist
 (ActiveRecord::NoDatabaseError)

in the rails root directory enter:

spring stop

it should say:

Spring stopped.

and the migration will work after that!

0bserver07
  • 3,390
  • 1
  • 28
  • 56
7

I am going to share what worked for me, it sounds like a joke, but it isn't.

I turned it on and off again.

What I mean by this is that I killed the Redis instance that need to run in parallel to my setup, stashed the local changes on the branch, and tried again. And it worked!

After that I killed the Unicorn server only, popped the stash to restore my local changes and restarted. And yes, it still worked.

This doesn't make any sense, I know and I apologise for not having a better explanation.

My only guess is that I might have kept the Unicorn and Redis servers running for too long (can't remember last time I shut down my Mac) and something might have gotten messed up. If that was the case then restarting the whole stack could have been the solution.

Hope this gives you, dear reader, food for thought on this mysterious error.

mokagio
  • 16,391
  • 3
  • 51
  • 58
  • 3
    Worked! I started getting this error on my local after my computer died. I just did a restart and let it kill processes gracefully and everything started running properly when it booted back up. – JLF Oct 24 '17 at 14:55
  • For me it was enough to quit and restart iterm2 - same issue before, also had the Mac running too long – ruby24 Jul 13 '18 at 21:33
4

The error is not related to your business data. Looking at the top lines of your log...

ActiveRecord::NoDatabaseError: local user with ID 501 does not exist

    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:661:in `rescue in connect'
    from /Users/toby/.rvm/gems/ruby-2.2.1/gems/activerecord-4.2.5.1/lib/active_record/connection_adapters/postgresql_adapter.rb:651:in `connect'

... it is clear that Rails cannot connect to the database.

The local user mentioned is likely an OS user which was configured to access postgres database and its record is either deleted or corrupted.

Try to connect to your database using psql from console and see if there are any issues. The command should look something like this.

psql --host=localhost --port=5432 --username=<your postgres user> --password --dbname=koboApi_development

Since your username is not set in your database config, it probably comes from an environment variable. Check that it does contain the proper value.

Nic Nilov
  • 5,056
  • 2
  • 22
  • 37
  • 1
    Very helpful. I could access the db and tables no problem, and the data was in there. Rails Server was working fine, but I couldn't access the db via Rails Console. Taking my lead from [this](http://stackoverflow.com/questions/33530287/when-i-try-to-do-rake-dbmigrate-i-get-an-error-activerecordnodatabaseerror) post, I went in and added `username` and `password` for my default postgres username, and it works. So it seems everything's fixed. Should I leave it at that, or should i dig deeper? pgAdmin3 lists my default user as owner of every db I have, and i don't have any other users.... – Mugshep Jul 06 '16 at 17:21
  • Depending on your configuration requirements you can leave it as is, it's a valid approach. If you have to use your system user to access postgres and need rails to pick it up automatically then you will probably have to do some digging into OS user records to figure it out. – Nic Nilov Jul 06 '16 at 17:24