I have the following sql statement and I want to refactor it to ORM but don't know how left join works when we have no foreign keys between two tables. It basically checks table sag_diff for new businessLines. It insert only new ones to table BusinessLines.
INSERT INTO tbl_BusinessLines
( BusinessLine_Name, Run_Date )
SELECT DISTINCT
tbl_SAG_Diff.BUSINESS_LINE, tbl_SAG_Diff.Run_Date
FROM tbl_SAG_Diff
LEFT JOIN tbl_BusinessLines
ON tbl_SAG_Diff.BUSINESS_LINE = tbl_BusinessLines.BusinessLine_Name
WHERE((tbl_BusinessLines.BusinessLine_Name)IS null);
here are my models:
class BusinessLines(models.Model):
BusinessLine_ID=models.IntegerField(primary_key=True)
BusinessLine_Name=models.CharField(max_length=100, null=True)
run_date = models.DateField(null=True)
class SAG_diff(models.Model):
Business_Line = models.CharField(max_length=255,null=True)
RUN_DATE = models.DateField(null=True)