I’ve been really trying hard to write a JOIN directly but it seems like that might not be possible based on this other thead.
Overall Goal
I have an ETL job that unifies 2 or 3 different datasources into a single table and I want to annotate a query with some “far away” data.
Models
# Source A
class SourceACompany(models.Model):
pass
class SourceADirector(models.Model):
company = models.ForeignKey(SourceACompany, on_delete=models.CASCADE)
# Source B
class SourceBCompany(models.Model):
pass
class SourceBDirector(models.Model):
company = models.ForeignKey(SourceBCompany, on_delete=models.CASCADE)
# Unified
class UnifiedCompany(models.Model):
source_a = models.OneToOneField(SourceACompany, on_delete=models.CASCADE)
source_b = models.OneToOneField(SourceBCompany, on_delete=models.CASCADE)
class UnifiedDirector(models.Model):
company = models.ForeignKey(UnifiedCompany, on_delete=models.CASCADE)
source_a = models.OneToOneField(SourceADirector, on_delete=models.CASCADE)
source_b = models.OneToOneField(SourceBDirector, on_delete=models.CASCADE)
Desired Query
At this point, I have ETL’ed Companies together and started to fill Directors. So, as I’m inserting a new UnifiedDirector, I want to find which UnifiedCompany it belongs to. My goal is to annotate the unified_company.id while I’m obtaining SourceADirector.
So the query would be:
SELECT source_a_directors.*, unified_company.id AS unified_company_id
FROM "source_a_directors"
JOIN "unified_company" ON "unified_company"."source_a_id" = "source_a_directors"."company_id"
The problem seems to be there is no direct FK relationship between UnifiedCompany and SourceADirector. Their columns are already being used in FK relationships for sourcing.
Questions
- Is it possible to write this join through ORM?
- Or, is it possible to relate
UnifiedCompanyandSourceADirectorby “reusing” the existing columns ("unified_company"."source_a_id" = "source_a_directors"."company_id") so that Django can know how to relate the two models directly? It seems like a trivial JOIN if Django knows how they’re related.
I tried looking at extra, and am really trying to avoid manual query because there are downstream filter+annotates.
Thanks so much for any help.