ActiveRecord无法删除记录

时间:2014-06-30 09:00:30

标签: postgresql ruby-on-rails-3.2

我有我的AttachmentsMembers表,其迁移为:

class AttachmentsMembers < ActiveRecord::Migration
  def change
    create_table :attachments_members, :id => false do |t|
      t.references :attachment, :null => false
      t.references :member, :null => false
    end

    add_index :attachments_members, [:attachment_id, :member_id]
  end
end

如果我尝试从控制台删除任何记录,则会收到以下错误:

ActiveRecord::StatementInvalid: PG::Error: ERROR:  zero-length delimited identifier at or near """"
LINE 1: ...OM "attachments_members" WHERE "attachments_members"."" = $1
                                                                ^
: DELETE FROM "attachments_members" WHERE "attachments_members"."" = $1
from /home/icicle/.rvm/gems/ruby-2.0.0-p353@ltbuddy/gems/activerecord-3.2.18/lib/active_record/connection_adapters/postgresql_adapter.rb:1208:in `prepare

附件代码会员模型:

class AttachmentsMember < ActiveRecord::Base
  #Relationships
  belongs_to :attachment
  belongs_to :member
end

在附件模型中:

  belongs_to :user, class_name: 'User', foreign_key: 'user_id'
  belongs_to :attachable, polymorphic: true
  belongs_to :referable, polymorphic: true

  has_many :attachments, as: :referable, :dependent => :destroy

  has_many :attachments_members, :dependent => :destroy
  has_many :members, :through => :attachments_members, :dependent => :destroy

在会员模型中:

  belongs_to :user, class_name: 'User', foreign_key: 'user_id'
  belongs_to :case, class_name: 'Case', foreign_key: 'case_id'
  belongs_to :user_type, class_name: 'UserType', foreign_key: 'user_type_id'

  has_many :attachments_members, dependent: :destroy
  has_many :attachments, :through => :attachments_members, dependent: :destroy

  has_many :documents_members, dependent: :destroy
  has_many :documents, :through => :documents_members, dependent: :destroy

即使从我的控制台执行AttachmentsMember.last,我也会收到相同的错误,但是AttachmentsMember.first正常工作。

有人解释问题的原因吗?

1 个答案:

答案 0 :(得分:1)

通过为AttachmentsMember表创建主键

解决了该问题