{"action":"created","issue":{"url":"https://api.github.com/repos/toptal/chewy/issues/214","labels_url":"https://api.github.com/repos/toptal/chewy/issues/214/labels{/name}","comments_url":"https://api.github.com/repos/toptal/chewy/issues/214/comments","events_url":"https://api.github.com/repos/toptal/chewy/issues/214/events","html_url":"https://github.com/toptal/chewy/issues/214","id":93944108,"number":214,"title":"Importing index generates queries with duplicate IDs","user":{"login":"giovannibonetti","id":3451581,"avatar_url":"https://avatars.githubusercontent.com/u/3451581?v=3","gravatar_id":"","url":"https://api.github.com/users/giovannibonetti","html_url":"https://github.com/giovannibonetti","followers_url":"https://api.github.com/users/giovannibonetti/followers","following_url":"https://api.github.com/users/giovannibonetti/following{/other_user}","gists_url":"https://api.github.com/users/giovannibonetti/gists{/gist_id}","starred_url":"https://api.github.com/users/giovannibonetti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/giovannibonetti/subscriptions","organizations_url":"https://api.github.com/users/giovannibonetti/orgs","repos_url":"https://api.github.com/users/giovannibonetti/repos","events_url":"https://api.github.com/users/giovannibonetti/events{/privacy}","received_events_url":"https://api.github.com/users/giovannibonetti/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-07-09T03:25:08Z","updated_at":"2015-07-09T04:41:34Z","closed_at":null,"body":"Hi! First of all, thanks for this library, it has been very useful in my company!\r\nI think it is better if I explain the issue with my use case:\r\n\r\n```ruby\r\nclass ServicesIndex < Chewy::Index\r\n  define_type define_type Service.includes(service_template: [], service_category: {category_template: [], business: {pictures: [], week_schedule: []} }, skills: {employment: {user: [], week_schedule: :shifts}}) do\r\n    field :id, :name\r\n    ...\r\n  end\r\n```\r\n```ruby\r\npry(main)> ServicesIndex.import\r\nSELECT  DISTINCT \"services\".\"id\", \"services\".\"id\" AS alias_0 FROM \"services\" LEFT OUTER JOIN \"service_templates\" ON \"service_templates\".\"id\" = \"services\".\"service_template_id\" LEFT OUTER JOIN \"service_categories\" ON \"service_categories\".\"id\" = \"services\".\"service_category_id\" LEFT OUTER JOIN \"category_templates\" ON \"category_templates\".\"id\" = \"service_categories\".\"category_template_id\" LEFT OUTER JOIN \"businesses\" ON \"businesses\".\"id\" = \"service_categories\".\"business_id\" LEFT OUTER JOIN \"pictures\" ON \"pictures\".\"depicted_id\" = \"businesses\".\"id\" AND \"pictures\".\"depicted_type\" = $1 LEFT OUTER JOIN \"week_schedules\" ON \"week_schedules\".\"schedulable_id\" = \"businesses\".\"id\" AND \"week_schedules\".\"schedulable_type\" = $2 LEFT OUTER JOIN \"skills\" ON \"skills\".\"service_id\" = \"services\".\"id\" LEFT OUTER JOIN \"employments\" ON \"employments\".\"id\" = \"skills\".\"employment_id\" LEFT OUTER JOIN \"users\" ON \"users\".\"id\" = \"employments\".\"user_id\" LEFT OUTER JOIN \"week_schedules\" \"week_schedules_employments\" ON \"week_schedules_employments\".\"schedulable_id\" = \"employments\".\"id\" AND \"week_schedules_employments\".\"schedulable_type\" = $3 LEFT OUTER JOIN \"shifts\" ON \"shifts\".\"week_schedule_id\" = \"week_schedules_employments\".\"id\"  ORDER BY \"services\".\"id\" ASC LIMIT 1000  [[\"depicted_type\", \"Business\"], [\"schedulable_type\", \"Business\"], [\"schedulable_type\", \"Employment\"]]\r\n   (2.9ms)  SELECT \"services\".\"id\" FROM \"services\" LEFT OUTER JOIN \"service_templates\" ON \"service_templates\".\"id\" = \"services\".\"service_template_id\" LEFT OUTER JOIN \"service_categories\" ON \"service_categories\".\"id\" = \"services\".\"service_category_id\" LEFT OUTER JOIN \"category_templates\" ON \"category_templates\".\"id\" = \"service_categories\".\"category_template_id\" LEFT OUTER JOIN \"businesses\" ON \"businesses\".\"id\" = \"service_categories\".\"business_id\" LEFT OUTER JOIN \"pictures\" ON \"pictures\".\"depicted_id\" = \"businesses\".\"id\" AND \"pictures\".\"depicted_type\" = $1 LEFT OUTER JOIN \"week_schedules\" ON \"week_schedules\".\"schedulable_id\" = \"businesses\".\"id\" AND \"week_schedules\".\"schedulable_type\" = $2 LEFT OUTER JOIN \"skills\" ON \"skills\".\"service_id\" = \"services\".\"id\" LEFT OUTER JOIN \"employments\" ON \"employments\".\"id\" = \"skills\".\"employment_id\" LEFT OUTER JOIN \"users\" ON \"users\".\"id\" = \"employments\".\"user_id\" LEFT OUTER JOIN \"week_schedules\" \"week_schedules_employments\" ON \"week_schedules_employments\".\"schedulable_id\" = \"employments\".\"id\" AND \"week_schedules_employments\".\"schedulable_type\" = $3 LEFT OUTER JOIN \"shifts\" ON \"shifts\".\"week_schedule_id\" = \"week_schedules_employments\".\"id\" WHERE \"services\".\"id\" IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)  ORDER BY \"services\".\"id\" ASC  [[\"depicted_type\", \"Business\"], [\"schedulable_type\", \"Business\"], [\"schedulable_type\", \"Employment\"]]\r\n  Service Load (1.8ms)  SELECT \"services\".* FROM \"services\" WHERE \"services\".\"id\" IN (1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6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 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 11, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 16, 16, 16, 16, 16, 16)\r\n```\r\n\r\nWhat is bothering me is that in the final line of the select statement the IDs of my table (services) are duplicated (many times). The problem seems to be that, although there is a \"DISTINCT\" in the first query, \r\nin the second query there isn't one. Am I doing something wrong or is this expected behavior?\r\n\r\nThanks in advance!"},"comment":{"url":"https://api.github.com/repos/toptal/chewy/issues/comments/119816679","html_url":"https://github.com/toptal/chewy/issues/214#issuecomment-119816679","issue_url":"https://api.github.com/repos/toptal/chewy/issues/214","id":119816679,"user":{"login":"pyromaniac","id":88852,"avatar_url":"https://avatars.githubusercontent.com/u/88852?v=3","gravatar_id":"","url":"https://api.github.com/users/pyromaniac","html_url":"https://github.com/pyromaniac","followers_url":"https://api.github.com/users/pyromaniac/followers","following_url":"https://api.github.com/users/pyromaniac/following{/other_user}","gists_url":"https://api.github.com/users/pyromaniac/gists{/gist_id}","starred_url":"https://api.github.com/users/pyromaniac/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pyromaniac/subscriptions","organizations_url":"https://api.github.com/users/pyromaniac/orgs","repos_url":"https://api.github.com/users/pyromaniac/repos","events_url":"https://api.github.com/users/pyromaniac/events{/privacy}","received_events_url":"https://api.github.com/users/pyromaniac/received_events","type":"User","site_admin":false},"created_at":"2015-07-09T04:41:33Z","updated_at":"2015-07-09T04:41:33Z","body":"Unfortunately not"}}