gpt4 book ai didi

mysql - 获取表不存在错误,但表确实存在(ActiveRecord::StatementInvalid Mysql2::Error: Table doesn't exist)

转载 作者:IT王子 更新时间:2023-10-28 23:47:36 25 4
gpt4 key购买 nike

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

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

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

为了让您了解我的问题的背景,我在几年前编写了这个应用程序,直到最近它都运行良好。当我尝试使用瘦服务器启动应用程序时,出现以下错误

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

因此,我向托管服务的技术支持寻求帮助。他们说,

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

他们更新了我所有的 gem 以使其正常工作。我最初使用的是 Rails 3.0.1 和 thin 1.2.7。现在我有 Rails 3.1.10 和 thin 1.5.1。现在应用程序正在加载,所有功能都在工作,Secretsanta 除外。

当我仔细查看错误消息的以下部分时,“SHOW FIELDS FROM secretsanta”我看到表名是“secretsanta”,它应该是“secretsantas”吗?

我不确定该怎么做,毕竟该应用程序在过去 3 年中运行良好,我不确定为什么在更新 gems 后它无法运行

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

secret 圣塔.rb

   class Secretsanta < ActiveRecord::Base
belongs_to :user
belongs_to :gift_receiver, :class_name => "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

如果您需要其他信息,请告诉我。提前感谢您的帮助

最佳答案

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

除非您在 secretsanta 模型上设置了 table_name,否则 Rails 将查找名为 secretsantas 的表。如果该应用程序之前运行正常,我猜该表实际上被命名为 secretsantas

要列出可用表,请运行:

tables = ActiveRecord::Base.connection.tables

啊,问题来了:

'Secretsanta'.pluralize
=> "Secretsanta"

尝试在模型中指定表名:

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

关于mysql - 获取表不存在错误,但表确实存在(ActiveRecord::StatementInvalid Mysql2::Error: Table doesn't exist),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19937286/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com