我最近不得不徹底改造最初使用MySQL的Django項目,但爲了部署到Heroku(根據他們的建議docs regarding Django deployment),我遷移了通過以下方法MySQL數據庫的Postgres:在從MySQL遷移到Postgres後,瞭解「SEPARATOR」處或附近的語法錯誤
安裝依賴關係:
pyscopg2
和py-mysql2pgsql
設置Postgres數據庫
個遷移數據庫:
$ py-mysql2pgsql
- 這將創建一個mysql2pgsql.yml
文件,它看起來像這樣:mysql: hostname: localhost port: 3306 socket: /tmp/mysql.sock username: foo password: bar database: your_database_name compress: false destination: postgres: hostname: localhost port: 5432 username: foo password: bar database: your_database_name
通過傳輸的數據:
$ py-mysql2pgsql -v -f mysql2pgsql.yml
的咖啡許多杯之後,部署Heroku成功了(大部分情況下,直到我今天發現這個問題),該應用程序按照預期在heroku上運行,並運行$ heroku local
。但今天,在前端訪問某個功能時,與數據庫中的錯誤在瀏覽器中出現:
DatabaseError at /projects/
syntax error at or near "SEPARATOR"
LINE 1: ...s, sc.name as sport_category, string_agg(au.email SEPARATOR ...
^
Request Method: GET
Request URL: http://0.0.0.0:8989/projects/
Django Version: 1.5.1
Exception Type: DatabaseError
Exception Value:
syntax error at or near "SEPARATOR"
LINE 1: ...s, sc.name as sport_category, string_agg(au.email SEPARATOR ...
我相信相關的代碼是在一個名爲business.py:
def getAllProjects(self):
'''
@note: Retrieve all projects
'''
from django.db import connection
cursor = connection.cursor()
cursor.execute("select p.id, p.title, p.description, p.deadline, pt.name as project_type, s.name as status, sc.name as sport_category, group_concat(au.email SEPARATOR '\n') as project_managers from project p left join project_manager_in_project pmip on p.id = pmip.project_id left join auth_user au on pmip.project_manager_id = au.id inner join project_type pt on p.projectType_id = pt.id inner join status s on p.status_id = s.id inner join sport_category sc on p.sportCategory_id = sc.id where p.deleted = 0 group by p.id")
projects = cursor.fetchall()
return projects
我知道,談論類似問題的幾個帖子:
Postgresql - concat_ws like function?
Postgresql GROUP_CONCAT equivalent?
這導致我試圖將group_concat(au.email SEPARATOR '\n')
更改爲string_agg(au.email SEPARATOR '\n')
,然後array_agg(au.email SEPARATOR '\n')
但我仍然收到相同的錯誤。
是否有另一種方法我應該這樣做,並調整此功能以實現解決方法?
UPDATE:任何人誰是有興趣的,所提供的選擇的答案(和評論)考慮我下面把我帶到下面改變查詢語句這沒有問題迄今的工作原理:
cursor.execute("select \"project\".\"id\", project.title, project.description, \"project\".\"deadline\", \"pt\".\"name\" as \"project_type\", \"sc\".\"name\" as status, \"sc\".\"name\" as sport_category, string_agg(au.email, E'\n') as project_managers from project left join project_manager_in_project pmip on project.id = pmip.project_id left join auth_user au on pmip.project_manager_id = au.id inner join project_type pt on project.\"projectType_id\" = pt.id inner join status s on project.\"status_id\" = \"s\".id inner join sport_category sc on \"project\".\"sportCategory_id\" = \"sc\".\"id\" where project.deleted = 0 group by 1,2,3,4,5,6,7")
由於這個應用程序的原始開發人員已創建所有的雙引號表,這導致了column p.projecttype_id does not exist
錯誤和其他類似的does not exist
錯誤,當我所做的一切都被刪除SEPARATOR
和group_concat
(兩者都不存在於Postgres)至string_agg
。換句話說,根據考慮我的建議,我不得不使用雙引號封裝幾乎所有的表名和列名(並且當然要避免使用引號)。
感謝您的建議。現在,當我嘗試加載該頁面時,在瀏覽器中收到此錯誤消息:'列p.projecttype_id不存在 LINE 2:..._ manager_id = au.id內部連接project_type pt on p.projectT ...' – AdjunctProfessorFalcon
錯誤非常簡單。你所引用的表中沒有列(表格項目中的projecttype_id) –
除了'project'表中有一個'projectType_id'列(類型爲Integer),所以我很困惑。 – AdjunctProfessorFalcon