Warm tip: This article is reproduced from stackoverflow.com, please click
django python sql

Django

发布于 2020-03-27 10:22:57

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)
Questioner
whoisearth
Viewed
103
greg 2014-01-04 01:55

Try to write query as:

"SELECT jobdep_id AS id ..."

maybe it helps.