dependent :: destroy导致postgresql错误

时间:2015-03-12 10:52:48

标签: ruby-on-rails-3.2 postgresql-9.3

class Accdist < ActiveRecord::Base
  has_many :accdistlavoraziones, dependent: :destroy

删除accdist时,控制台会输出以下内容:

 Accdist Load (27.3ms)  SELECT "accdists".* FROM "accdists" WHERE "accdists"."id" = $1 LIMIT 1  [["id", "1"]]
 Accdistlavorazione Load (20.2ms)  SELECT "accdistlavoraziones".* FROM "accdistlavoraziones" WHERE "accdistlavoraziones"."accdist_id" = 1
  SQL (59.7ms)  DELETE FROM "accdistlavoraziones" WHERE "accdistlavoraziones"."" = $1  [[nil, nil]]
PG::SyntaxError: ERROR:  zero-length delimited identifier at or near """"
LINE 1: ...OM "accdistlavoraziones" WHERE "accdistlavoraziones"."" = $1
                                                                ^
: DELETE FROM "accdistlavoraziones" WHERE "accdistlavoraziones"."" = $1
   (0.1ms)  ROLLBACK
Completed 500 Internal Server Error in 155.3ms

奇怪的是,如果我查询:

Accdistlavorazione.where('accdist_id = ?', 1).count
   (0.6ms)  SELECT COUNT(*) FROM "accdistlavoraziones" WHERE (accdist_id = 1)
 => 10

正在返回有效结果。

为什么导致postgresql处理的内部机制为nil(s)?

1 个答案:

答案 0 :(得分:4)

对于遇到这种情况的人来说,可能发生的是以下情况 - 通过rails快捷方式has_and_belongs_to_many创建连接表 - 意识到您稍后需要has_many ... through:设置 - 实际运行创建新连接记录的突变...因为正在引用[...]_id - 来删除时间,你正在寻找连接表的ID,自然就是nil