简体   繁体   English

Rails给关系表带来问题,SQLite3 :: SQLException:没有这样的列

[英]Rails giving problems with relational table, SQLite3::SQLException: no such column

Can't seem to find a solution to this one. 似乎找不到解决方案。 I have a relational table, BlabsUser, from which I'm trying to find and destroy a record. 我有一个关系表BlabsUser,试图从中查找并销毁一条记录。 I'm doing this: 我正在这样做:

BlabsUser.find_by_user_id_and_blab_id(1,29).destroy

And getting this lovely error: 并得到这个可爱的错误:

BlabsUser Load (0.2ms)  SELECT "blabs_users".* FROM "blabs_users" WHERE "blabs_users"."user_id" = 1 AND "blabs_users"."blab_id" = 29 LIMIT 1
Could not log "sql.active_record" event. NoMethodError: undefined method `name' for nil:NilClass
SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: blabs_users.: DELETE FROM "blabs_users" WHERE "blabs_users"."" = ?
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `initialize'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `new'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/sqlite3-1.3.5/lib/sqlite3/database.rb:91:in `prepare'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:234:in `block in exec_query'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:223:in `exec_query'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/sqlite_adapter.rb:249:in `exec_delete'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:101:in `delete'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `delete'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/relation.rb:351:in `delete_all'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/persistence.rb:94:in `destroy'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/locking/optimistic.rb:119:in `destroy'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `block in destroy'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:390:in `_run_destroy_callbacks'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:254:in `destroy'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `block in destroy'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/transactions.rb:236:in `destroy'
    from (irb):1
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
    from /Users/dylandrop/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'

Why is it looking for a column called blabs_users? 为什么要查找名为blabs_users的列? Why isn't it just looking for a record in my blabs_users table? 为什么不只是在blabs_users表中查找记录? Also, more importantly, how do I fix this? 另外,更重要的是,我该如何解决?

Edit: And I've also tried this with the same problem: 编辑:并且我也尝试了同样的问题:

BlabsUser.destroy_all(:blab_id => 29, :user_id => 1)

Edit 2, as per request: My model is just 根据要求编辑2:我的模型只是

class BlabsUser < ActiveRecord::Base
end

Migration is just 迁移只是

class BlabsUsers < ActiveRecord::Migration
  def self.up
      create_table :blabs_users, :id => false, :force => true do |t|
          t.integer :user_id
          t.integer :blab_id
          t.timestamps
      end
  end

  def self.down
      drop_table :blabs_users
  end
end

ActiveRecord depends pretty heavily on having a unique key to work with (usually the primary key of the table) Since you explicitly don't have one defined ( :id => false ), I strongly suspect that your problem is related to that. ActiveRecord在很大程度上依赖于要使用唯一键(通常是表的主键),因为您没有明确定义键( :id => false ),因此我强烈怀疑您的问题与此相关。

destroy() is also documented as Destroy an object (or multiple objects) that has the given id which strongly suggests it needs that unique id to work with. destroy()也记录为销毁具有给定id的一个对象(或多个对象),强烈建议它需要使用唯一的id。

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

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