1

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)

1 Answer 1

3

You can run raw SQL queries with Django's ORM if that's what you wanted. You don't have to change your query in that case, you can check documentation here: https://docs.djangoproject.com/en/2.0/topics/db/sql/#performing-raw-sql-queries

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.