繁体   English   中英

Rails 4-Has_many通过-StatementInvalid-SQLite3 :: SQLException:无此类列:

[英]Rails 4 - Has_many through - StatementInvalid - SQLite3::SQLException: no such column:

我在这个问题上苦苦挣扎了4天,我想知道我是否没有遇到ActiveRecord错误? 我正在尝试将用户模型链接到标注模型。

user.rb

class User < ActiveRecord::Base
    has_many :callouts_users
    has_many :callouts, through: :callouts_users    
    devise  :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable     
    has_many :posts, inverse_of: :creator
    has_many :callouts, as: :calloutable    
    has_many :profiles, as: :profileable, :validate => true     
    validates :name, presence: true
    validates_uniqueness_of :name, :case_sensitive => false, :message => "This name has already been taken"
end

callout.rb

class Callout < ActiveRecord::Base
    has_many :callouts_users
    has_many :users, through: :callouts_users
    belongs_to :conversation
    belongs_to :calloutable, polymorphic: true, class_name: "::Callout", :validate => true  
    validates :conversation, presence: true
    validates :calloutable, presence: true
    validates_uniqueness_of :calloutable_id, :scope => [:user_id, :conversation_id, :calloutable_type]
end

user_callout.rb

class UserCallout < ActiveRecord::Base
    belongs_to :user
    belongs_to :callout
    validates :type, presence: true, 
    validates :type, :inclusion=> { :in => ["up", "down"] }
end

我的迁移如下:

..._ create_callouts_users.rb

class CreateCalloutsUsers < ActiveRecord::Migration
  def change
    create_table :callouts_users do |t|
      t.timestamps null: false
    end
  end
end

..._ add_callout_to_callouts_users.rb

class AddCalloutToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :callout, index: true
    add_foreign_key :callouts_users, :callouts
  end
end

..._ add_user_to_callouts_users.rb

class AddUserToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :user, index: true
    add_foreign_key :callouts_users, :users
  end
end

当我尝试做类似的事情

@callout = @conversation.callouts.find_by(calloutable: @user) 
if(@callout.nil?) @callout = Callout.new(conversation: @conversation, calloutable: @user)
@callout.users << current_user
@callout.save

我立即拥有:CalloutsController#create SQLite3 :: SQLException中的ActiveRecord :: StatementInvalid:没有这样的列:callouts.user_id:在“ callouts” WHERE(“ callouts”。“ calloutable_id”为NULL和“ callouts”中选择1 AS)。 user_id“是NULL AND”标注“。” conversation_id“是NULL AND”标注“。” calloutable_type“是NULL)LIMIT 1

好像ActiverRecords在哪里在我的标注表上查找“ user_id”列,而user_id仅在联接表侧...我在模型上做错了吗? 为什么我的has_many-槽关联未被识别?

这是生成的SQL代码:

用户存在(0.2ms)在“用户”中以1作为1(LOWER(“ users”。“ name”)= LOWER('name10')AND“ users ..” id“!= 10)LIMIT 1

存在标注(0.6ms)从“标注”中选择1为1(“标注”。“ calloutable_id”为NULL和“标注”。“ user_id”为NULL和“标注”。“ conversation_id” = 1和“标注”。 “ calloutable_type”为NULL)LIMIT 1

SQLite3 :: SQLException:没有这样的列:callouts.user_id:从“ callouts” WHERE(“ callouts”。“ calloutable_id” IS NULL AND“ callouts”。“ user_id” IS NULL AND“ callouts”。“ conversation_id”作为一个1 = 1 AND“ callouts”。“ calloutable_type” IS NULL)LIMIT 1

(0.0ms)回滚事务

在50毫秒内完成500个内部服务器错误

ActiveRecord::Schema.define(version: 20150720002524) do

  create_table "callouts", force: :cascade do |t|
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "conversation_id"
    t.integer  "calloutable_id"
    t.string   "calloutable_type"
  end

  add_index "callouts", ["calloutable_type", "calloutable_id"], name: "index_callouts_on_calloutable_type_and_calloutable_id"
  add_index "callouts", ["conversation_id"], name: "index_callouts_on_conversation_id"

  create_table "callouts_users", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "callout_id"
  end

  add_index "callouts_users", ["callout_id"], name: "index_callouts_users_on_callout_id"
  add_index "callouts_users", ["user_id"], name: "index_callouts_users_on_user_id"

  create_table "conversations", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.datetime "created_at",      null: false
   t.datetime "updated_at",      null: false
    t.integer  "conversation_id"
    t.integer  "creator_id"
    t.text     "title"
    t.text     "content"
 end

  add_index "posts", ["conversation_id"], name: "index_posts_on_conversation_id"
  add_index "posts", ["creator_id"], name: "index_posts_on_creator_id"

  create_table "potential_users", force: :cascade do |t|
    t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
  end

  create_table "profiles", force: :cascade do |t|
   t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "profileable_id"
    t.string   "profileable_type"
    t.string   "description"
 end

  add_index "profiles", ["description"], name: "index_profiles_on_description", unique: true
  add_index "profiles", ["profileable_type", "profileable_id"], name: "index_profiles_on_profileable_type_and_profileable_id"

  create_table "users", force: :cascade do |t|
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "name"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

================================================== =>我在这个问题上苦苦挣扎了4天,我想知道我是否没有遇到ActiveRecord错误? 我正在尝试将用户模型链接到标注模型。

user.rb

class User < ActiveRecord::Base
    has_many :callouts_users
    has_many :callouts, through: :callouts_users    
    devise  :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable     
    has_many :posts, inverse_of: :creator
    has_many :callouts, as: :calloutable    
    has_many :profiles, as: :profileable, :validate => true     
    validates :name, presence: true
    validates_uniqueness_of :name, :case_sensitive => false, :message => "This name has already been taken"
end

callout.rb

class Callout < ActiveRecord::Base
    has_many :callouts_users
    has_many :users, through: :callouts_users
    belongs_to :conversation
    belongs_to :calloutable, polymorphic: true, class_name: "::Callout", :validate => true  
    validates :conversation, presence: true
    validates :calloutable, presence: true
    validates_uniqueness_of :calloutable_id, :scope => [:user_id, :conversation_id, :calloutable_type]
end

user_callout.rb

class UserCallout < ActiveRecord::Base
    belongs_to :user
    belongs_to :callout
    validates :type, presence: true, 
    validates :type, :inclusion=> { :in => ["up", "down"] }
end

我的迁移如下:

..._ create_callouts_users.rb

class CreateCalloutsUsers < ActiveRecord::Migration
  def change
    create_table :callouts_users do |t|
      t.timestamps null: false
    end
  end
end

..._ add_callout_to_callouts_users.rb

class AddCalloutToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :callout, index: true
    add_foreign_key :callouts_users, :callouts
  end
end

..._ add_user_to_callouts_users.rb

class AddUserToCalloutsUsers < ActiveRecord::Migration
  def change
    add_reference :callouts_users, :user, index: true
    add_foreign_key :callouts_users, :users
  end
end

当我尝试做类似的事情

@callout = @conversation.callouts.find_by(calloutable: @user) 
if(@callout.nil?) @callout = Callout.new(conversation: @conversation, calloutable: @user)
@callout.users << current_user
@callout.save

我立即拥有:CalloutsController#create SQLite3 :: SQLException中的ActiveRecord :: StatementInvalid:没有这样的列:callouts.user_id:在“ callouts” WHERE(“ callouts”。“ calloutable_id”为NULL和“ callouts”中选择1 AS)。 user_id“是NULL AND”标注“。” conversation_id“是NULL AND”标注“。” calloutable_type“是NULL)LIMIT 1

好像ActiverRecords在哪里在我的标注表上查找“ user_id”列,而user_id仅在联接表侧...我在模型上做错了吗? 为什么我的has_many-槽关联未被识别?

这是生成的SQL代码:

用户存在(0.2ms)在“用户”中以1作为1(LOWER(“ users”。“ name”)= LOWER('name10')AND“ users ..” id“!= 10)LIMIT 1

存在标注(0.6ms)从“标注”中选择1为1(“标注”。“ calloutable_id”为NULL和“标注”。“ user_id”为NULL和“标注”。“ conversation_id” = 1和“标注”。 “ calloutable_type”为NULL)LIMIT 1

SQLite3 :: SQLException:没有这样的列:callouts.user_id:从“ callouts” WHERE(“ callouts”。“ calloutable_id” IS NULL AND“ callouts”。“ user_id” IS NULL AND“ callouts”。“ conversation_id”作为一个1 = 1 AND“ callouts”。“ calloutable_type” IS NULL)LIMIT 1

(0.0ms)回滚事务

在50毫秒内完成500个内部服务器错误

ActiveRecord::Schema.define(version: 20150720002524) do

  create_table "callouts", force: :cascade do |t|
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "conversation_id"
    t.integer  "calloutable_id"
    t.string   "calloutable_type"
  end

  add_index "callouts", ["calloutable_type", "calloutable_id"], name: "index_callouts_on_calloutable_type_and_calloutable_id"
  add_index "callouts", ["conversation_id"], name: "index_callouts_on_conversation_id"

  create_table "callouts_users", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.integer  "callout_id"
  end

  add_index "callouts_users", ["callout_id"], name: "index_callouts_users_on_callout_id"
  add_index "callouts_users", ["user_id"], name: "index_callouts_users_on_user_id"

  create_table "conversations", force: :cascade do |t|
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

  create_table "posts", force: :cascade do |t|
    t.datetime "created_at",      null: false
   t.datetime "updated_at",      null: false
    t.integer  "conversation_id"
    t.integer  "creator_id"
    t.text     "title"
    t.text     "content"
 end

  add_index "posts", ["conversation_id"], name: "index_posts_on_conversation_id"
  add_index "posts", ["creator_id"], name: "index_posts_on_creator_id"

  create_table "potential_users", force: :cascade do |t|
    t.datetime "created_at", null: false
   t.datetime "updated_at", null: false
  end

  create_table "profiles", force: :cascade do |t|
   t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.integer  "profileable_id"
    t.string   "profileable_type"
    t.string   "description"
 end

  add_index "profiles", ["description"], name: "index_profiles_on_description", unique: true
  add_index "profiles", ["profileable_type", "profileable_id"], name: "index_profiles_on_profileable_type_and_profileable_id"

  create_table "users", force: :cascade do |t|
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "name"
  end

  add_index "users", ["email"], name: "index_users_on_email", unique: true
  add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true

end

===========================>编辑

好,

我有另一个线索。 我认为该错误出在用户模型中,但我不知道如何编写。

简而言之:

1.)不同的用户可以参加不同的标注。 因此,用户<=>标注是一个“ has_many /通过”关系。 (不是HABTM,因为我需要自定义联接模型)。 所以我可以写@callout.users

2.)一个Callout对象指向一个Calloutable(Calloutable是User或PotentialUser)。 但是Calloutable可能由不同的Callout作为目标。 因此,这是一个longate_to / has_many多态关系。 我可以写@ user.callouts ...也可以写@callout.users ...

但是:在情况1)或2)中的@ callout.users并不意味着同一件事。

以下是详细的模型:

class Callout < ActiveRecord::Base
    has_many :callouts_users
    has_many :users, through: :callouts_users

    belongs_to :calloutable, polymorphic: true, class_name: "::Callout", :validate => true    
    validates :calloutable, presence: true
    validates_uniqueness_of :calloutable_id, :scope => [:user_id, :conversation_id, :calloutable_type]

    belongs_to :conversation
    validates :conversation, presence: true
end

class CalloutsUser < ActiveRecord::Base
    belongs_to :user
    belongs_to :callout
    validates_uniqueness_of :user_id, :scope => [:callout_id]
end

class User < ActiveRecord::Base
    has_many :callouts_users
    has_many :callouts, through: :callouts_users
    has_many :callouts, as: :calloutable

    devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable
    has_many :posts, inverse_of: :creator    
    has_many :profiles, as: :profileable, :validate => true 

    validates :name, presence: true
    validates_uniqueness_of :name, :case_sensitive => false, :message => "This name has already been taken"
end

class PotentialUser < ActiveRecord::Base
    has_many :callouts, as: :calloutable      
    has_one :profile, as: :profileable, :validate => true
    validates :profile, presence: true
end

有什么想法可以重写用户模型部分吗? (2 has_many:callouts)我想我只需要改变用户收到的@ user.callouts和用户发出的@ user.callouts之间的关系...

我觉得好像您已经做到了这比需要做的难。 不要打架,它可以为您提供帮助!

首先,对于您的联接表,使用标准的Rails命名约定:按字母顺序对模型进行排序。 如果您回滚一点,而不是为一个操作创建三个迁移,为什么不这样做:

rails g migration callouts_users callout_id:integer user_id:integer

再加上模型中具有has_and_belongs_to_many关系,那么您应该一切顺利。

它是固定的! 问题实际上是我写的:

has_many :callouts_users
has_many :callouts, through: :callouts_users
has_many :callouts, as: :calloutable

所以我定义了has_many :callouts,两次。 当然,Rails不知道如何理解@callout.users

与:

has_many :callouts_users
has_many :callouts, through: :callouts_users, source: "call", class_name: "Call"
has_many :callins, as: :callable, class_name: "Call"`

我工作完美! 感谢您的耐心和理解,因为我是新人... :-)


[英]#<ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.sender_id:

暂无
暂无

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

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