Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Table doesn't exist error, but the table does exist (ActiveRecord::StatementInvalid Mysql2::Error: Table doesn't exist)

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'

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)

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

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.

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

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

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

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

    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

like image 477
Ruper Avatar asked Nov 12 '13 18:11

Ruper


1 Answers

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.

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
like image 136
nbrew Avatar answered Oct 19 '22 14:10

nbrew