2016-06-21 122 views
-2

我需要這個查詢的一些幫助。將postgres查詢轉換爲django查詢

SELECT s.nombre as 'habilidad', 
     GROUP_CONCAT(a.carrera_id) 
FROM perfiles_alumno a, 
     perfiles_alumno_habilidades h, 
     perfiles_universidad u, 
     segmentacion_habilidad s 
WHERE u.user_id = %s 
AND  a.universidad_id = u.id 
AND  a.id = h.alumno_id 
AND  s.id = h.habilidad_id 
GROUP BY h.habilidad_id 
ORDER BY a.carrera_id 

我需要在Django中執行此查詢。我可以在SQLite3中運行的本地環境中使用它,但是當我推到生產環境時,它失敗了,因爲我有Postgres。

有沒有人可以幫助我?

回答

0

要將此SQL轉換爲Postgres,請將group_concat更改爲string_agg(s.carrera_id, ',')。但是,它不再適用於SQLite。如果您在開發和生產中使用相同的數據庫,您可能會遭受更少的損失。如果你想知道每個組的s.nombre,我想你需要GROUP BY h.habilidad_id, s.nombre

此外,您應該避免使用用戶提供的值插入%s,因爲這是安全風險。