获取表并不存在错误,但是表确实存在(ActiveRecord::StatementInvalid Mysql2:: error:表不存在)

时间:2022-08-29 22:57:45

I am getting the following error when I try to access one of my pages

当我试图访问我的一个页面时,我得到了以下错误。

ActiveRecord::StatementInvalid (Mysql2::Error: Table 'p478r679_partybot.secretsanta' doesn't exist: SHOW FIELDS FROM 'secretsanta'): app/controllers/secretsantas_controller.rb:7:in `index'

ActiveRecord::StatementInvalid p478r679_partybot(Mysql2::错误:表”。secretsanta不存在:显示来自“secretsanta”的字段:app/控制器/secretsantas_controller。rb:7:“指数”

But this table exists in my DB.

但是这个表存在于我的数据库中。

Just to give you a background of my problem, I wrote this application few years ago and it was working fine up until recently. When I tried to start the application with thin server, I got the following error

为了给你一个我的问题的背景,我几年前就写了这个应用程序,直到最近它一直在工作。当我尝试用瘦服务器启动应用程序时,我得到了以下错误。

You have already activated rack 1.4.3, but your Gemfile requires rack 1.2.1. Using bundle exec may solve this. (Gem::LoadError)

您已经激活了机架1.4.3,但是您的Gemfile需要机架1.2.1。使用bundle exec可以解决这个问题。(Gem::LoadError)

So I approached the tech support from my hosting service for assistance. They said,

所以我从托管服务中心寻求技术支持。他们说,

“The problem was that the rails version was not compatible with the bundler version. Now we have changed the rails version to "3.1.10" and have installed the bundler "1.2.3 ". Now your website is loading fine.The "thin" service is started in your server. The port is listening to the application. Refer the details given below.”

问题是rails版本与bundler版本不兼容。现在我们已经将rails版本改为“3.1.10”,并且已经安装了bundler“1.2.3”。现在你的网站正在加载。“瘦”服务在您的服务器中启动。端口正在监听应用程序。请参考下面给出的详细信息。

And they have updated all my gems for get it working. I was originally using Rails 3.0.1 and thin 1.2.7. Now I have Rails 3.1.10 and thin 1.5.1. Now the application is loading, and all the features working, except Secretsanta.

他们更新了我所有的宝石,让它工作。我最初使用的是Rails 3.0.1和1.2.7。现在我有了Rails 3.1.10和瘦1.5.1。现在应用程序正在加载,所有的特性都在工作,除了Secretsanta。

When I look at the following part of the error message closely, “SHOW FIELDS FROM secretsanta” I see the table name is “secretsanta”, should it be “secretsantas”?

当我仔细查看错误消息的以下部分时,“从secretsanta显示字段”,我看到表名是“secretsanta”,应该是“secretsantas”吗?

I am not sure what to make of this, after all the application was working fine for the past 3 years and I am not sure why it not working after updating the gems

我不知道该怎么做,因为在过去的3年里,所有的应用程序都运行良好,我不确定为什么它在更新了gems之后就不工作了。

Here is some code snip of Secretsanta model User model and SecretsantasController.

这里是一些代码snip的秘密,模型用户模型和秘密控制器。

Secretsanta.rb

Secretsanta.rb

   class Secretsanta < ActiveRecord::Base
     belongs_to :user
     belongs_to :gift_receiver, :class_name => "User"
     ...

User.rb

User.rb

    class User < ActiveRecord::Base

      # Setup accessible (or protected) attributes for your model
      attr_accessible :first_name, :last_name, :email,
                      :password, :password_confirmation, :remember_me

      validates :first_name, :presence => true
      validates :last_name, :presence => true
      validate :should_be_invited

      # Include default devise modules. Others available are:
      # :token_authenticatable, :confirmable, :lockable and :timeoutable
      devise :database_authenticatable, :registerable,
             :recoverable, :rememberable, :trackable, :validatable

      has_one :potluck

      has_one :secretsanta
      has_one :gift_receiver, :through => :secretsanta

      has_many :secretsantaExclutions
      has_many :excluded, :through => :secretsantaExclutions

      has_many :discussions
      has_many :comments
      ...

secretsantas_controller.rb

secretsantas_controller.rb

    class SecretsantasController < ApplicationController
      before_filter :authenticate_user!
      def index
        @exclutionList = SecretsantaExclution.find_all_by_user_id(current_user.id)
        @event = Event.find_by_id(4)
        @rsvp = GuestList.find_by_guest(current_user.email)
        @secretsanta = Secretsanta.find_by_user_id(current_user.id) #i am getting the error at this line

      end

Please let me know if you need and additional information. Thanks for your help in advance

如果需要的话,请告诉我。谢谢你的帮助。

1 个解决方案

#1


15  

I believe you were right on in your suspicion about the secretsanta table name.

我相信你对这个秘密的表名的怀疑是正确的。

Unless you've set a table_name on your secretsanta model, rails will look for a table named secretsantas. If the application was working before, I'd guess that the table actually is named secretsantas.

除非您在您的secretsanta模型上设置了table_name,否则rails将寻找一个名为secretsantas的表。如果应用程序以前工作过,我猜这个表实际上是名为secretsantas。

To list the available tables, run:

要列出可用的表,请运行:

tables = ActiveRecord::Base.connection.tables

Ah, here's the problem:

啊,问题是:

'Secretsanta'.pluralize
=> "Secretsanta"

Try specifying the table name in your model:

尝试在模型中指定表名:

class Secretsanta < ActiveRecord::Base
  self.table_name = "secretsantas"
end

#1


15  

I believe you were right on in your suspicion about the secretsanta table name.

我相信你对这个秘密的表名的怀疑是正确的。

Unless you've set a table_name on your secretsanta model, rails will look for a table named secretsantas. If the application was working before, I'd guess that the table actually is named secretsantas.

除非您在您的secretsanta模型上设置了table_name,否则rails将寻找一个名为secretsantas的表。如果应用程序以前工作过,我猜这个表实际上是名为secretsantas。

To list the available tables, run:

要列出可用的表,请运行:

tables = ActiveRecord::Base.connection.tables

Ah, here's the problem:

啊,问题是:

'Secretsanta'.pluralize
=> "Secretsanta"

Try specifying the table name in your model:

尝试在模型中指定表名:

class Secretsanta < ActiveRecord::Base
  self.table_name = "secretsantas"
end