【发布时间】:2018-02-03 03:22:18
【问题描述】:
我一直在开发一个 Rails 应用程序,但最近我遇到了这个错误,我不知道它为什么会发生,它只发生在我的开发环境中,在生产环境中它工作得很好。 它涉及从内部连接查询数据,这是错误:
SQLite3::SQLException: no such column: answers.answers_project_id: SELECT 1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."answers_project_id" = "answers_projects"."id" WHERE "answers_projects"."project_id" = ? AND "answers"."question_id" = ? LIMIT ?
在视图中的这一行中出现了(在这个页面中,我试图显示与当前项目对应的所有问题):
<%if @proyecto.answers.exists?(question_id: pregunta.id) %>
以下是所有模型和控制器的所有相关行: 答案模型
class Answer < ActiveRecord::Base
belongs_to :question
has_many :answers_projects
has_many :projects, through: :answers_projects
validates :answer, presence: true, length: {minimum: 1}
def get_question_text
Question.where(id: self.id).question
end
end
回答控制器:
class AnswersController < ApplicationController
before_action :require_user
before_action :get_current_project
...
ANSWERS_PROJECTS 模型:
class AnswersProject < ActiveRecord::Base
belongs_to :project, :dependent => :destroy
has_many :answer, :dependent => :destroy
end
ANSWERS_PROJECTS 控制器(无约束):
class AnswersProjectsController < ApplicationController
end
项目模型
class Project < ActiveRecord::Base
belongs_to :user
has_many :answers_projects, :dependent => :destroy
has_many :answers, through: :answers_projects, :dependent => :destroy
accepts_nested_attributes_for :answers
项目控制器
class ProjectsController < ApplicationController
before_action :set_project, only: [:edit,:update,:show,:destroy]
before_action :require_user
before_action :require_same_user, only: [:edit, :show, :update, :destroy]
before_action :require_admin, only: [:edit,:update,:destroy]
问题模型:
class Question < ApplicationRecord
has_many :options, dependent: :destroy
has_many :answers, dependent: :destroy
accepts_nested_attributes_for :options, allow_destroy: true, reject_if: proc { |att| att['description'].blank? }
问题控制器:
class QuestionsController < ApplicationController
before_action :require_user
before_action :require_project
before_action :require_user, except: [:new, :create]
before_action :current_project, only: [:index]
我的代码与在生产中运行的代码完全相同,我尝试回滚多个版本并再次克隆存储库,重置并重新创建数据库并手动将值插入每个表中,但错误仍然存在,它出现了从昨天开始似乎是随机的,我可以很好地在开发中运行测试,所以任何帮助都将不胜感激。
[更新]
尝试使用完全相同的代码访问完全相同的视图时,heroku 日志和我的本地控制台日志不同:
HEROKU 日志:
2018-02-03T03:35:49.030139+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] Processing by QuestionsController#index as
2018-02-03T03:35:49.085812+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2 [["id", 1], ["LIMIT", 1]]
2018-02-03T03:35:49.117275+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] (0.9ms) SELECT COUNT(*) FROM "projects" WHERE "projects"."user_id" = $1 [["user_id", 1]]
2018-02-03T03:35:49.121949+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] Project Load (0.6ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2 [["id", 2], ["LIMIT", 1]]
2018-02-03T03:35:49.148596+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] Rendering questions/index.html.erb within layouts/application
2018-02-03T03:35:49.161477+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] Question Load (1.7ms) SELECT "questions".* FROM "questions" ORDER BY "questions"."process" ASC
2018-02-03T03:35:49.204998+00:00 app[web.1]: [97a6f665-a618-4d88-b018-0b3329e6b4c5] Answer Exists (1.4ms) SELECT 1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."id" = "answers_projects"."answer_id" WHERE "answers_projects"."project_id" = $1 AND "answers"."question_id" = $2 LIMIT $3 [["project_id", 2], ["question_id", 3], ["LIMIT", 1]]
我的控制台日志:
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
(0.2ms) SELECT COUNT(*) FROM "projects" WHERE "projects"."user_id" = ? [["user_id", 1]]
Project Load (0.2ms) SELECT "projects".* FROM "projects" WHERE "projects"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]]
Rendering questions/index.html.erb within layouts/application
Question Load (0.3ms) SELECT "questions".* FROM "questions" ORDER BY "questions"."process" ASC
Answer Exists (0.6ms) SELECT 1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."answers_project_id" = "answers_projects"."id" WHERE "answers_projects"."project_id" = ? AND "answers"."question_id" = ? LIMIT ? [["project_id", 3], ["question_id", 34], ["LIMIT", 1]]
Rendered questions/_answer_form.html.erb (131.3ms)
Rendered questions/index.html.erb within layouts/application (143.0ms)
Completed 500 Internal Server Error in 418ms (ActiveRecord: 4.8ms)
正如你所看到的,这两个句子在不同的环境中使用相同的代码会产生不同的结果,也许错误就在这里?:
赫罗库:Answer Exists (1.4ms) SELECT 1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."id" = "answers_projects"."answer_id" WHERE "answers_projects"."project_id" = $1 AND "answers"."question_id" = $2 LIMIT $3 [["project_id", 2], ["question_id", 3], ["LIMIT", 1]]
开发:Answer Exists (0.6ms) SELECT 1 AS one FROM "answers" INNER JOIN "answers_projects" ON "answers"."answers_project_id" = "answers_projects"."id" WHERE "answers_projects"."project_id" = ? AND "answers"."question_id" = ? LIMIT ? [["project_id", 3], ["question_id", 34], ["LIMIT", 1]]
[更新]
如果我尝试访问相同的视图而没有创建任何问题,则视图会呈现,但在创建一个问题后会出现错误。
【问题讨论】:
标签: ruby-on-rails ruby activerecord sqlexception sqlite3-ruby