关于ruby on rails:ActiveRecord :: NoDatabaseError:具有ID的本地用户不存在

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

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

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

我环顾四周,并没有看到任何关于这个特定错误的线程(他们似乎都在寻找显示页面的个别记录) - 虽然这个,这个线程(通过扩展)看起来最接近。检查我正在使用的数据库上的用户显示错误中的用户ID("501")不存在。

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

ps - 如果它是相关的,我使用rake db:rollback按照这个线程回滚我的迁移。

提前致谢。

Rails控制台

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
.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

1
2
3
4
5
6
7
8
9
10
11
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 | {}

从我的架构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

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

1
2
/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.

并且迁移将在那之后工作!


我将分享对我有用的东西,这听起来像个笑话,但事实并非如此。

我再次打开和关闭它。

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

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

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

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

亲爱的读者,希望这能让你对这个神秘的错误有所了解。


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

1
2
3
4
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连接到您的数据库,看看是否有任何问题。命令看起来应该是这样的。

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

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