【问题标题】:ActiveRecord::StatementInvalid - SQLite3::SQLException: no such columnActiveRecord::StatementInvalid - SQLite3::SQLException: 没有这样的列
【发布时间】:2013-10-09 08:06:49
【问题描述】:

在 RSpec 中运行测试时,出现以下错误:

Failures:

  1) User micropost associations should have the right micropost in the right order
     ←[31mFailure/Error:←[0m ←[31m@user.microposts.should == [newer_micropost, older_micropost]←[0m
     ←[31mActiveRecord::StatementInvalid:←[0m
       ←[31mSQLite3::SQLException: no such column: micropost.created_at: SELECT "microposts".* FROM "microposts"  WHERE "microposts"."user_id" = 1 ORD
ER BY micropost.created_at DESC←[0m
←[36m     # C:in `load_target'←[0m
←[36m     # ./spec/models/user_spec.rb:153:in `block (3 levels) in <top (required)>'←[0m

  2) User micropost associations should destroy associated microposts
     ←[31mFailure/Error:←[0m ←[31mmicroposts = @user.microposts.dup←[0m
     ←[31mActiveRecord::StatementInvalid:←[0m
       ←[31mSQLite3::SQLException: no such column: micropost.created_at: SELECT "microposts".* FROM "microposts"  WHERE "microposts"."user_id" = 1 ORD
ER BY micropost.created_at DESC←[0m
←[36m     # C:in `load_target'←[0m
←[36m     # ./spec/models/user_spec.rb:157:in `block (3 levels) in <top (required)>'←[0m

Finished in 5.18 seconds
←[31m97 examples, 2 failures←[0m

user_spec.rb:

require 'spec_helper'

describe User do

  before do 
    @user = User.new(name: "John Smith", email: "john@example.com", 
                                     password: "password", password_confirmation: "password")
  end

  subject { @user }

  it { should respond_to(:name) }
  it { should respond_to(:email) }
  it { should respond_to(:password_digest) }
  it { should respond_to(:password) }
  it { should respond_to(:password_confirmation) }
  it { should respond_to(:remember_token) }
  it { should respond_to(:admin) }
  it { should respond_to(:authenticate) }
  it { should respond_to(:microposts) }

  it { should be_valid }
  it { should_not be_admin }

  describe "micropost associations" do

    before { @user.save }
    let!(:older_micropost) do 
      FactoryGirl.create(:micropost, user: @user, created_at: 1.day.ago)
    end
    let!(:newer_micropost) do
      FactoryGirl.create(:micropost, user: @user, created_at: 1.hour.ago)
    end

    it "should have the right micropost in the right order" do
      # user.micropost returns an array
      @user.microposts.should == [newer_micropost, older_micropost]
    end

    it "should destroy associated microposts" do
      microposts = @user.microposts.dup
      @user.destroy
      microposts.should_not be_empty
      microposts.each do |micropost|
        Micropost.find_by_id(micropost.id).should be_nil
      end
    end
  end
end

schema.rb(缩短):

ActiveRecord::Schema.define(:version => 20131002154740) do

  create_table "microposts", :force => true do |t|
    t.string   "content"
    t.integer  "user_id"
    t.datetime "created_at", :null => false
    t.datetime "updated_at", :null => false
  end

  add_index "microposts", ["user_id", "created_at"], :name => "index_microposts_on_user_id_and_created_at"

  create_table "users", :force => true do |t|
    .
    .
    .
    .
  end

end

微贴模型:

class Micropost < ActiveRecord::Base
    attr_accessible :content
    belongs_to :user

    validates :user_id, presence: true

    # Ensures the microposts are in descending (DESC) order
    # from newest to oldest, (SQL syntax).
    default_scope order: 'micropost.created_at DESC'
end

我已经运行了bundle exec rake db:migratebundle exec rake db:test:prepare,我还重置了数据库并再次尝试了,但是没有成功。

具体错误为:SQLite3::SQLException: no such column: micropost.created_at: SELECT "microposts".* FROM "microposts" WHERE "microposts"."user_id" = 1 ORD ER BY micropost.created_at DESC

【问题讨论】:

    标签: ruby-on-rails rspec sqlite


    【解决方案1】:

    好吧,SQL 的问题是排序列上的表名是单数而不是复数。

    即“ORDER BY micropost.created_at”应改为“ORDER BY microposts.created_at”。

    如果您还发布了您的 Micropost 和 User 模型源代码,我或许能够判断这是由您的模型代码中的错误还是 ActiveRecord 中的错误引起的。

    您使用的是哪个版本的 Rails?

    【讨论】:

    • 我意识到这是我在 Micropost 模型中输入的default_scope order: 'micropost.created_at DESC',我应该使用复数。
    猜你喜欢
    • 1970-01-01
    • 2011-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-24
    • 2015-10-08
    • 2016-12-08
    • 1970-01-01
    相关资源
    最近更新 更多