Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Raw query must include the primary key

Tags:

python

sql

django

There is a similar question here - Raw query must include the primary key

However I'm running off of a legacy DB and therefore can't figure out what the issue is with the Primary Key.

This is my RAW query -

trg = Trgjob.objects.db_manager('AdmiralDEV').raw("""
    SELECT jobdep_id, jm.jobmst_id, jobdep_type, (jm1.jobmst_prntname + '\' + jm1.jobmst_name) AS jobdep_jobmst,
    jobdep_operator, jobdep_status, jobdep_joblogic, jobdep_ingroup, jobdep_dateoffset, jobdep_instoffset,
    jobdep_canignore, jobdep_filename, jobdep_filetype, jobdep_fileextent, nodmst_id, varmst_id, jobdep_value
    FROM Jobdep jd
    INNER JOIN Jobmst jm ON jd.jobmst_id = jm.jobmst_id
    INNER JOIN Jobmst jm1 ON jd.jobdep_jobmst = jm1.jobmst_id
    WHERE jm.jobmst_id = 9878""")

On the DB works fine, but in django I get the following failure -

Raw query must include the primary key

The primary key on this model is "jobdep_id" as seen in the models.py here -

class Jobdep(models.Model):
    jobdep_id = models.IntegerField(primary_key=True)
like image 623
whoisearth Avatar asked Nov 29 '22 00:11

whoisearth


1 Answers

Try to write query as:

"SELECT jobdep_id AS id ..."

maybe it helps.

like image 192
greg Avatar answered Dec 10 '22 02:12

greg