【问题标题】:Why i get error no such column?为什么我得到错误没有这样的列?
【发布时间】:2015-08-17 22:04:21
【问题描述】:

我有公司模型和模型 Invintaton,公司可以邀请其他公司进行消息传递,现在我只需要显示确认消息传递的公司

class Company < ActiveRecord::Base
  has_many :sent_invitations, class_name: 'Invintation', foreign_key: 'sender_id'
  has_many :invitation_recipients, through: :sent_invitations, source: :recipient 
  has_many :incoming_invitations, class_name: 'Invintation', foreign_key: 'recipient_id'
  has_many :invitation_senders, through: :incoming_invitations, source: :sender
end 

class Invintation < ActiveRecord::Base
  belongs_to :recipient, class_name: 'Company', foreign_key: 'recipient_id'
  belongs_to :sender, class_name: 'Company', foreign_key: 'sender_id'
end

class MessagesController < ApplicationController


  def new
    @company = Company.find(params[:company_id])
    @message = @company.sent_messages.new
    @recipients = Company.joins(:invitation_recipients).where(invitation_recipients: {sender_id: @company.id, confrm: true})
  end
end

但我收到错误 SQLite3::SQLException: no such column: sent_invitations.confrm: SELECT "companies".* FROM "companies" INNER JOIN "invintations" ON "invintations"."sender_id" = "companies"."id " WHERE "sent_invitations"."confrm" = 't'

我知道这种方式只显示发送邀请的公司,这是必要的,包括邀请

【问题讨论】:

  • 你的schema.rb呢?你有这些模型的迁移吗?
  • 该列是否存在?您是否运行了所有迁移?此外,“invintations”拼写错误——帮自己一个忙,现在就改正它。
  • create_table "invintations", force: true do |t| t.integer "sender_id" t.integer "recipient_id" t.integer "author_id" t.boolean "confirm",默认:false t.datetime "created_at" t.datetime "updated_at" end
  • 是的,所有迁移都运行了

标签: ruby-on-rails ruby-on-rails-4


【解决方案1】:

您的查询中至少有两个拼写错误:

  • sent_invitations.confrm 而应该是 sent_invitations.confirm
  • Invintation 而应该是 Invitation

检查您的代码并修正拼写错误,假设生成的数据库架构没有出现相同的拼写错误。

【讨论】:

  • SQLite3::SQLException: 没有这样的列:sent_invintations.confrm: SELECT "companies".* FROM "companies" INNER JOIN "invintations" ON "invintations"."sender_id" = "companies"." id" WHERE "sent_invintations"."confrm" = 't'
  • 在销毁模型 Invintation 并创建模型 Invitation rails 显示错误 SQLite3::SQLException: no such column: sent_invitations.confrm: SELECT "companies".* FROM "companies" INNER JOIN "invitations" ON "invitations "."sender_id" = "companies"."id" WHERE "sent_invitations"."confrm" = 't'
  • 在我的回复中,我提到了两个错字。您确定该列名为 confrm 而不是 confirm
  • 重命名 confrm 以确认结果相同
  • 我认为错误是因为我没有表 sent_invintations
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-25
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-10
相关资源
最近更新 更多