簡體   English   中英

獲取表不存在錯誤,但表確實存在(ActiveRecord :: StatementInvalid Mysql2 :: Error:Table不存在)

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

當我嘗試訪問其中一個頁面時出現以下錯誤

ActiveRecord :: StatementInvalid(Mysql2 ::錯誤:表'p478r679_partybot.secretsanta'不存在:顯示來自'secretsanta'的信息):app / controllers / secretsantas_controller.rb:7:in`index'

但是這個表存在於我的數據庫中。

為了給你一個問題的背景,幾年前我寫了這個應用程序,直到最近才開始工作。 當我嘗試使用瘦服務器啟動應用程序時,出現以下錯誤

您已經激活了機架1.4.3,但您的Gemfile需要機架1.2.1。 使用bundle exec可以解決這個問題。 (寶石:: LoadError)

所以我從托管服務處尋求技術支持以尋求幫助。 他們說,

“問題是rails版本與bundler版本不兼容。 現在我們已將rails版本更改為“3.1.10”並已安裝捆綁器“1.2.3”。 現在您的網站正在加載正常。“瘦”服務在您的服務器中啟動。 端口正在偵聽應用程序。 請參閱下面給出的詳細信息。“

他們已經更新了我的所有寶石以使其正常運行。 我最初使用的是Rails 3.0.1和瘦1.2.7。 現在我有Rails 3.1.10和瘦1.5.1。 現在應用程序正在加載,除Secretsanta外,所有功能都在運行。

當我仔細查看錯誤信息的以下部分時,“顯示來自秘密的信息”我看到表名是“secretsanta”,它應該是“secretsanta s ”嗎?

我不知道該怎么做,畢竟應用程序在過去的3年里工作正常,我不確定為什么它在更新寶石后不起作用

以下是Secretsanta模型用戶模型和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

如果您需要和其他信息,請告訴我。 感謝您的幫助

我相信你對secrettsanta表名的懷疑是正確的。

除非您在secretsanta模型上設置table_name,否則rails將查找名為secretsantas的表。 如果應用程序之前正在運行,我猜這個表實際上被命名為secretsantas

要列出可用表,請運行:

tables = ActiveRecord::Base.connection.tables

啊,這是問題所在:

'Secretsanta'.pluralize
=> "Secretsanta"

嘗試在模型中指定表名:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM