【问题标题】:Rails NoSuchColumn error on INNER joins内部连接上的 Rails NoSuchColumn 错误
【发布时间】: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


    【解决方案1】:

    这对吗?

    class AnswersProject < ActiveRecord::Base
      belongs_to :project, :dependent => :destroy
      has_many :answer, :dependent => :destroy
    end 
    

    AnswersProjects 是否有_many :answers?还是应该是belongs_to?如果是has_many,则必须复数。

    【讨论】:

    • 感谢您指出这一点,AnswersProjects 有很多答案,因为它将一个项目与许多答案联系起来,我已经对其进行了复数化,但问题仍然存在,您能否检查一下我刚刚添加的 heroku 日志?
    • 我真的认为你不需要 has_many 那里,因为它是一个连接表。您有项目和答案作为主要模型。 AnswersProject 是答案和项目之间的链接。它应该有belongs_to project 和belongs_to answer,这允许每个项目有很多答案,每个答案有很多项目。我检查了新的日志。看来您没有相同的代码。看来您有答案 belongs_to :answers_projects 正在开发中(但这不是您发布的内容)
    • 我将我的 has_many 更改为 AnswersProject 中的 belongs_to 但问题仍然存在,我将 heroku 应用程序克隆到一个新文件夹中,并在此处发布了控制器,因此它必须与生产中的代码相同,尽管我不得不执行几次heroku回滚,难道是最新的预回滚版本被克隆而不是旧版本吗?例如:我在第 26 版,但我回滚到 24,也许克隆的版本是 26?
    • 问题是数据库必须反映您的模型。如果不更改数据库架构,就无法更改模型。在最后一次更改中,您需要 AnswersProjects 表中的 project_id 和 answer_id。也许您的数据库架构和模型之间存在差异。
    猜你喜欢
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多