简体   繁体   English

RoR-SQLite3 :: SQLException:没有这样的列:-Rails不将_id附加到生成的查询中

[英]RoR - SQLite3::SQLException: no such column: - Rails not appending _id to generated query

I have two model objects LineItemClass and LineItemSubClass. 我有两个模型对象LineItemClass和LineItemSubClass。 I am trying to add a new LineItemSubClass to a LineItemClass, and it is generating an SQL error: 我试图将新的LineItemSubClass添加到LineItemClass,并且它生成SQL错误:

SQLite3::SQLException: no such column: line_item_sub_classes.line_item_class.

The correct query should be something more like line_item_sub_classes.line_item_class_id . 正确的查询应该更像line_item_sub_classes.line_item_class_id

I used generate to create everything, so I am fairly confused as to why this is happening. 我使用generate来创建所有内容,因此对于为什么会发生这种情况我很困惑。 In researching the problem, everywhere else I have seen the SQL eception, the generated query had _id. 在研究问题时,我在其他任何地方都看到过SQL消息,生成的查询具有_id。 My only thought is that I am doing something wrong with resources that have multiple word names - are there any good docs on conventions for multiple word names? 我唯一的想法是我对具有多个单词名称的资源做错了-是否有关于多个单词名称的约定的好文档?

schema.rb schema.rb

ActiveRecord::Schema.define(:version => 20130905194234) do
  create_`enter code here`table "line_item_classes", :force => true do |t|
    t.string   "code"
    t.string   "name"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  create_table "line_item_sub_classes", :force => true do |t|
    t.string   "code"
    t.string   "name"
    t.integer  "line_item_class_id"
    t.datetime "created_at",         :null => false
    t.datetime "updated_at",         :null => false
  end

  add_index "line_item_sub_classes", ["line_item_class_id"], :name =>"index_line_item_sub_classes_on_line_item_class_id"
end

line_item_class.rb line_item_class.rb

class LineItemClass < ActiveRecord::Base 
  include ActiveModel::ForbiddenAttributesProtection

  has_many :line_item_sub_classes

  attr_accessible :code, :name

  validates :code, presence: true, length: {is: 2}, uniqueness: true
  validates :name, presence: true, length: {maximum: 100}, uniqueness: true
end

line_item_sub_class.rb line_item_sub_class.rb

class LineItemSubClass < ActiveRecord::Base
  include ActiveModel::ForbiddenAttributesProtection

  belongs_to :line_item_class

  attr_accessible :code, :name

  validates :code, presence: true, length: {is: 2}, :uniqueness => {:scope => :line_item_class}
  validates :name, presence: true, length: {maximum: 100}, :uniqueness => {:scope => :line_item_class}
end

offending code from line_item_sub_classes_controller.rb 来自line_item_sub_classes_controller.rb的有害代码

class LineItemSubClassesController < ApplicationController
  def create
    @line_item_class = LineItemClass.find(params[:line_item_class_id])
    @line_item_sub_class = 
      @line_item_class.line_item_sub_classes.
      create(params[:line_item_sub_class].permit(:code, :name))
    redirect_to line_item_class_path(@line_item_class)
  end
end

from the view, here is the form section - this is what creates the params right? 从视图上看,这是表单部分-这是创建参数的原因吗?

<%= form_for([@line_item_class,
              @line_item_class.line_item_sub_classes.build]) do |f| %>
  <p>
    <%= f.label :code %><br />
    <%= f.text_field :code %>
  </p>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.submit %>
  </p>
<% end %>

the form tag in the output html is 输出html中的form标签是

<form accept-charset="UTF-8" action="/line_item_classes/1/line_item_sub_classes" class="new_line_item_sub_class" id="new_line_item_sub_class" method="post">

The error is being generated by the controller, in line 该错误是由控制器在一行中生成的

@line_item_sub_class = @line_item_class.line_item_sub_classes.create(params[:line_item_sub_class].permit(:code, :name))

I tried splitting this into two lines, but it didn't help. 我尝试将其分为两行,但没有帮助。 The update line threw a nil object exception. 更新行抛出nil对象异常。

@line_item_sub_class =@line_item_class.line_item_sub_classes.new
@line_item_sub_class.update_attributes(params[:line_item_sub_class].permit(:code, :name))

full error is; 完全错误是;

SQLite3::SQLException: no such column: line_item_sub_classes.line_item_class: SELECT 1 AS one FROM "line_item_sub_classes" WHERE ("line_item_sub_classes"."code" = 'A0' AND "line_item_sub_classes"."line_item_class" IS NULL) LIMIT 1. 

I don't have a clue what the railsmagic is doing here, but I can't imagine why this query would ever be needed. 我不知道铁匠在这里做什么,但我无法想象为什么会需要此查询。 It seems like it couldn't be run until after the record existed. 似乎只有在记录存在后才能运行。

ANSWER - SORT OF... I have fixed the error by changing the scope of the uniqueness validators in the LineItemSubClass model. 答案-排序问题...我通过更改LineItemSubClass模型中唯一性验证器的范围来修复错误。

  validates :code, presence: true, length: {is: 2}, :uniqueness => {:scope => :line_item_class}
  validates :name, presence: true, length: {maximum: 100}, :uniqueness => {:scope => :line_item_class}

is now 就是现在

  validates :code, presence: true, length: {is: 2}, :uniqueness => {:scope => :line_item_class_id}
  validates :name, presence: true, length: {maximum: 100}, :uniqueness => {:scope => :line_item_class_id}

This works. 这可行。 I can now create a nested LineItemSubClass without an error, but my RoR-sense is tingling. 现在,我可以创建一个嵌套的LineItemSubClass,而不会出现错误,但是我的RoR感觉有点刺痛。 It seems like Rails should be able to infer the name of the relevant database column when I scope uniqueness to an existing association. 当我将唯一性的范围限定为现有关联时,Rails似乎应该能够推断相关数据库列的名称。 Coding the name of a database column seems fragile. 对数据库列的名称进行编码似乎很脆弱。 Am I doing something wrong? 难道我做错了什么?

So, your parameters are: 因此,您的参数是:

{ "utf8"=>"✓", 
  "authenticity_token"=>"Lj8iy9VkzdWrvN4LyzXQ5BY7FdpV57l12Euqtch9RtQ=",
  "line_item_sub_class"=>{   // These are the only parameters that will get
    "code"=>"A0",            // considered when creating your new
    "name"=>"Panel Box"      // LineItemSubClass record in the DB
  }, 
  "commit"=>"Create Line item sub class",
  "action"=>"create",
  "controller"=>"line_item_sub_classes",
  "line_item_class_id"=>"1"  // But this parameter needs to be included
                             // needs to be included to set the
                             // line_item_sub_classes.line_item_class_id field
}

It looks like, in your controller, you're using params[:line_item_sub_class] to create the new record. 看起来,在您的控制器中,您正在使用params[:line_item_sub_class]创建新记录。 So, I think first of all that you need to change your form so that... 所以,我认为首先您需要更改表格,以便...

params[:line_item_class_id]

...is actually under ...实际上在

params[:line_item_sub_class][:line_item_class_id]

That will take you part of the way there. 那将带您进入那里。 However, I don't think that will necessarily fix your entire issue. 但是,我认为这不一定能解决您的整个问题。 Can you update your question with: 您可以通过以下方式更新您的问题:

  1. the SQL query output from this action being called 此操作的SQL查询输出被调用
  2. the filename and line number on which you're getting this error 您收到此错误的文件名和行号

If you want you can just post the entire rails log output when this controller action is called. 如果需要,可以在调用此控制器操作时发布整个rails日志输出。 It thank that will demystify it enough (I hope) to get it solved. 它感谢它将使它足够神秘(我希望)能解决它。

Another way to say this is that your form is passing in a structure of parameters that don't quite seem to line up with what your controller is expecting, and that's probably most of the problem. 换句话说 ,您的表单正在传入的参数结构似乎与您的控制器期望的不一致,这可能是大多数问题。 So, believe it or not, look at your view, the HTML it produces, and try to figure out why the parameters in it don't match what rails is expecting in terms of structure and naming. 因此,信不信由你,查看您的视图,它生成的HTML,并尝试找出为什么其中的参数与rails在结构和命名方面期望的不匹配的原因。 If you can figure that out you'll be most of the way there. 如果您能弄清楚,那将是您的最佳选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM