Skip to content
This repository has been archived by the owner on Apr 17, 2018. It is now read-only.

Querying via Many-to-Many relationships uses bad key #11

Open
amedveczki opened this issue Dec 31, 2011 · 2 comments
Open

Querying via Many-to-Many relationships uses bad key #11

amedveczki opened this issue Dec 31, 2011 · 2 comments

Comments

@amedveczki
Copy link

See this thread:
http://groups.google.com/group/datamapper/browse_thread/thread/47c04728de93b2ee

If I have 2 models (Sentence, Word), which have a Many-to-Many
relationship, and Word has a field 'word', the following query:

Sentence.all(:words => { :word => "Hello" })

Tries to match "sentences.id" to "words.id" (live example at
http://pastebin.ca/2097242 ) :

FROM "sentences" 
WHERE "id" IN 
    (SELECT "words"."id"    # Query result will be a single words.id, 
which will be used as sentence id 
     FROM "words" 
     INNER JOIN "sentence_words" ON "words"."id" = 
"sentence_words"."word_id" 
     INNER JOIN "sentences" ON "sentence_words"."sentence_id" = 
"sentences"."id" 
     WHERE "words"."word" = 'Hello') 
ORDER BY "id" 

@alexwestholm
Copy link

Has there been any movement on this? This seems like a pretty serious problem.

@timmillwood
Copy link

I have the same issue:

@contents = Content.all(:type => 'blog', :published => true, :tags => {:tag => tag}, :order => [ :created.desc ], :limit => 5)

is creating

SELECT "id", "type", "title", "created", "alias", "legacy_tags", "published" FROM "contents" WHERE ("type" = 'blog' AND "published" = TRUE AND "id" IN (SELECT "tags"."id" FROM "tags" INNER JOIN "content_tags" ON "tags"."id" = "content_tags"."tag_id" INNER JOIN "contents" ON "content_tags"."content_id" = "contents"."id" WHERE "tags"."tag" = 'drupal')) ORDER BY "created" DESC LIMIT 5

Not ideal!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants