2012-09-27 103 views
0

我該如何去除表中與datamapper沒有關係的所有記錄?有問題的表是Post和Tag表(在多對多關係中),我想刪除所有沒有標籤的帖子。出於某種原因,試圖Post.all(:tags => nil)時,出現此錯誤:Datamapper:刪除所有沒有關係的記錄

DataObjects::SyntaxError: ERROR: operator does not exist: integer = character varying 
LINE 1: ...amp", "comment_count" FROM "posts" WHERE NOT("id" IN (SELECT... 
                  ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
(code: 52461700, sql state: 42883, query: SELECT "id", "timestamp", "source_network", "source_post_id", "source_user_id", "source_link", "img_full_url", "img_thumb_url", "approved", "review_timestamp", "comment_count" FROM "posts" WHERE NOT("id" IN (SELECT "tags"."name" FROM "tags" INNER JOIN "post_tags" ON "tags"."name" = "post_tags"."tag_name" INNER JOIN "posts" ON "post_tags"."post_id" = "posts"."id")) ORDER BY "id", uri: postgres:[email protected]/hash_tagging?scheme=postgres&user=tom&password=&host=localhost&port=&path=/hash_tagging&query=&fragment=&adapter=postgres) 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `execute_reader' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `block in read' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:141:in `read' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/repository.rb:162:in `read' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/collection.rb:1117:in `lazy_load' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/support/lazy_array.rb:409:in `each' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/collection.rb:504:in `each' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/collection.rb:976:in `map' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/dm-core-1.2.0/lib/dm-core/collection.rb:976:in `inspect' 
    from /Users/tom/.rbenv/versions/1.9.3-p194/bin/irb:12:in `<main>' 

有誰知道我做錯了嗎?

P.S.標籤表只有一列(名字,這是一個關鍵的),而關係是自動生成的DataMapper的

回答

1

改變一個NOT("id" IN"id" NOT IN

DELETE FROM posts WHERE id NOT IN (SELECT id FROM tags) 
+0

具有相同錯誤仍然出現指向'IN'位 –