获取表不存在错误,但表确实存在(ActiveRecord :: StatementInvalid Mysql2 :: Error:表不存在) [英] Getting Table doesn't exist error, but the table does exist (ActiveRecord::StatementInvalid Mysql2::Error: Table doesn't exist)

查看:466
本文介绍了获取表不存在错误,但表确实存在(ActiveRecord :: StatementInvalid Mysql2 :: Error:表不存在)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试访问我的页面之一时出现以下错误

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

ActiveRecord :: StatementInvalid(Mysql2 :: Error:表'p478r679_partybot.secretsanta'不存在:从'secretsanta'中显示字段): app/controllers/secretsantas_controller.rb:7:in'index'

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

但是该表存在于我的数据库中.

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

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

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

因此,我从托管服务处寻求技术支持以寻求帮助.他们说,

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

问题是rails版本与捆绑程序版本不兼容.现在,我们已将rails版本更改为"3.1.10",并安装了捆绑程序"1.2.3".现在您的网站可以正常加载.瘦"服务已在您的服务器中启动.端口正在监听应用程序.请参阅下面提供的详细信息."

"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 3.0.1和Thin 1.2.7.现在我有Rails 3.1.10和瘦的1.5.1.现在,应用程序正在加载,并且所有功能都可以使用,除了Secretsanta.

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.

当我仔细查看错误消息的以下部分时,显示来自secretsanta的字段",我看到表名是"secretsanta",应该是"secretsanta s "吗?

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"?

在过去3年中所有应用程序都能正常工作之后,我不确定该怎么做.我不确定为什么更新宝石后它无法正常工作

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

这是Secretsanta模型用户模型和SecretsantasController的一些代码片段.

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

Secretsanta.rb

Secretsanta.rb

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

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

推荐答案

我相信您对secretsanta表名的怀疑是正确的.

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

除非您在secretsanta模型上设置了table_name,否则Rails会查找名为secretsantas的表.如果应用程序以前可以运行,我猜该表实际上名为secretsantas.

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.

要列出可用表,请运行:

To list the available tables, run:

tables = ActiveRecord::Base.connection.tables

嗯,这是问题所在:

'Secretsanta'.pluralize
=> "Secretsanta"

尝试在模型中指定表名:

Try specifying the table name in your model:

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

这篇关于获取表不存在错误,但表确实存在(ActiveRecord :: StatementInvalid Mysql2 :: Error:表不存在)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆