I am running following query inside one of my classes which takes ORM classes in so that it can work with several similar tables.
(
self.db.query(self.orm_contact_class)
.options(
load_only(
self.orm_contact_class.id,
self.orm_contact_class.name,
self.orm_contact_class.email_attempts_dict,
),
joinedload(
self.orm_contact_class.__dict__[self.access_from_contact_to_company]
).load_only(self.orm_company_class.domain)
)
.where(
self.orm_contact_class.email == None,
self.orm_contact_class.name != None,
self.orm_company_class.domain != None,
catch_all_conditions
)
)
This results in this monstrous query:
SELECT test.crunchbase_people.id AS test_crunchbase_people_id,
test.crunchbase_people.name AS test_crunchbase_people_name,
test.crunchbase_people.email_attempts_dict AS test_crunchbase_people_email_attempts_dict,
crunchbase_companies_1.id AS crunchbase_companies_1_id,
crunchbase_companies_1.domain AS crunchbase_companies_1_domain
FROM test.crunchbase_companies,
test.crunchbase_people
LEFT OUTER JOIN (
test.crunchbase_people_crunchbase_companies AS crunchbase_people_crunchbase_companies_1
JOIN test.crunchbase_companies AS crunchbase_companies_1
ON crunchbase_companies_1.id = crunchbase_people_crunchbase_companies_1.crunchbase_companies_id)
ON test.crunchbase_people.id = crunchbase_people_crunchbase_companies_1.crunchbase_people_id
WHERE test.crunchbase_people.email IS NULL AND test.crunchbase_people.name IS NOT NULL AND test.crunchbase_companies.domain IS NOT NULL AND (test.crunchbase_companies.is_domain_catch_all = false OR test.crunchbase_companies.is_domain_catch_all IS NULL)
Which ends up never completing, and if I run it inside Postgres console it just returns duplicates of the exact same row, over and over again!
So it never gets to mapping the objects because the query simply takes forever. There is an easy way of doing this without ORM, and the query runs in 0.5 seconds (and no duplicates like above), but then my objects are not mapped which would cause me to refactor a lot of my code.
Does anyone know what could be the problem with such a query?