ActiveRecord :: NoDatabaseError:具有ID的本地用户不存在

时间:2016-07-06 12:55:20

标签: ruby-on-rails activerecord rails-activerecord

开发一个仅限API的Rails应用程序来使用API​​中的数据,过滤掉许多字段,然后广播我们想要公开的字段。

我的应用正常返回数据,但我意识到在源API级别上,我们更改了我们要公开的数据字段之一的数据类型。为了适应,我提交了我的工作,检查了一个新分支,运行迁移以更改数据字段的数据类型,我决定对它的工作方式感到满意,回滚迁移,承诺和签出主分支。该应用正常返回数据。但是,现在当我使用.first.last等查找程序方法在Rails控制台中测试我的模型记录时,我收到以下错误。他们之前工作正常。

我环顾四周,并没有看到任何有关此特定错误的线索(它们似乎都在寻找显示页面的个别记录) - 尽管this和{{3线程(通过扩展名)似乎最接近。并且使用db I m来检查数据库上的用户显示错误中的用户ID(" 501")不存在。

我做了什么以及我需要做什么才能再次调用finder方法?我是否需要在我的数据库中的错误中创建引用的用户? (为什么它不是我一直用来连接到我的数据库的默认用户?错误中的用户ID来自哪里?它甚至重要吗?)

ps - 如果相关,我使用rake db:rollback按照this帖子回滚我的迁移。

提前致谢。

Rails控制台

.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'

我肯定会收到我的记录 - 他们会进入数据库:

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 | {}
来自我的架构的

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

我的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

3 个答案:

答案 0 :(得分:64)

如果你收到这个错误,那似乎是Spring的问题,而不是数据库:

/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)

在rails根目录下输入:

spring stop

它应该说:

Spring stopped.

之后迁移将会有效!

答案 1 :(得分:6)

我将分享对我有用的东西,这听起来像个笑话,但它不是。

我再打开和关闭

我的意思是我杀死了需要与我的设置并行运行的Redis实例,隐藏了分支上的本地更改,并再次尝试。它有效!

之后我只杀了Unicorn服务器,弹出存储以恢复我的本地更改并重新启动。是的,它仍然奏效。

这没有任何意义,我知道并且因为没有更好的解释而道歉。

我唯一的猜测是,我可能让Unicorn和Redis服务器运行时间过长(上次我关闭Mac时无法记住),而且可能已经搞砸了。如果是这种情况,那么重新启动整个堆栈可能就是解决方案。

希望这能让你,亲爱的读者,为这个神秘的错误提供思考。

答案 2 :(得分:3)

该错误与您的商家数据无关。查看日志的顶行...

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'

......很明显Rails无法连接到数据库。

提到的本地用户很可能是一个OS用户,它被配置为访问postgres数据库并且其记录被删除或损坏。

尝试使用控制台中的psql连接到您的数据库,看看是否有任何问题。命令看起来应该是这样的。

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

由于您的数据库配置中未设置username,因此它可能来自环境变量。检查它是否包含正确的值。