ActiveRecord中的表名损坏错误

Har*_*tty 8 ruby activerecord ruby-on-rails puma

偶尔我们PG::UndefinedTable在使用ActiveRecord时会出错.关联表名称是一些损坏的方式,我经常看到 Cancelled附加到表名的末尾.

例如:

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "fooCancell" does not exist 
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "Cancelled" does not exist
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "barC" does not exist
Run Code Online (Sandbox Code Playgroud)

在上面的示例中,我使用foo和模糊了表名bar.

当rails项目在Puma中运行时,我们会看到这个错误.队列工作人员似乎做得很好.

错误消息中的表与实际表或模型不对应.看起来像是内存损坏的情况.有没有人见过这样的问题?如果是这样,你是如何解决它的?

puma.rb

on_worker_boot do
  ActiveRecord::Base.establish_connection
end
Run Code Online (Sandbox Code Playgroud)

database.yml的

production:
  url:  <%= ENV["DATABASE_URL"] %>
  pool: <%= ENV['DB_CONNECTION_POOL_SIZE'] || 5%>
  reaping_frequency: <%= ENV['DB_CONNECTION_REAPING_FREQUENCY'] || 10 %>
  prepared_statements: false
Run Code Online (Sandbox Code Playgroud)

Mys*_*yst 1

基于这个可能相关的错误,我在这里冒险猜测......

但你可能是:

  1. 在您的应用程序中调用fork;或者
  2. 在服务器 (puma) 分叉其工作进程之前(在应用程序初始化期间)调用 ActiveRecord 例程(使用数据库调用)。

其中任何一个都会破坏 ActiveRecord 的同步并导致多个进程共享数据库连接池而不同步其使用(导致交错和损坏的数据库命令)。

如果您使用的是fork,请确保关闭所有 ActiveRecord 数据库连接并重新初始化连接池(有一个函数调用可以执行此操作,但我不记得了,也许ActiveRecord.disconnect!是 或ActiveRecord.connection_pool.disconnect!)。

否则,在运行 Puma 之前(无论是在初始化过程中还是使用 Puma 的after_fork),请关闭所有 ActiveRecord 数据库连接并重新初始化连接池。