2
的時候所以我有教育水平的字典:KeyError異常查詢SQL鍊金術數據庫
education_levels = {
"Less than high school": 0,
"High school diploma or equivalent": 1,
"Postsecondary non-degree award": 2,
"Some college, no degree": 3,
"Associate's degree": 4,
"Bachelor's degree": 5,
"Master's degree": 6,
"Doctoral or professional degree": 7,
"#N/A": 100
}
我想查詢我的數據庫,以便它只能說明教育水平是比輸入的教育水平等於或更低。
jobs = Occupation.query.filter(Occupation.area_name == area).filter(
education_levels[Occupation.typical_entry_level_education] <= education_levels[education])
但是,這給了我一個關鍵錯誤。我知道一個關鍵錯誤就是當你看起來不是關鍵時。我100%肯定我已經覆蓋了數據庫中所有可能的密鑰。我甚至一直試圖尋找的東西,這不是他們中的按鍵使用此:
def test():
jobs = Occupation.query.all()
job_list = []
for job in jobs:
if not(job.typical_entry_level_education in education_levels.keys()):
d = {
'ed': job.typical_entry_level_education
}
job_list.append(d)
return job_list
輸出我跟我的測試得到()函數是這個
{
"jobs": []
}
我無法捉摸如何如果所有內容都位於鍵集中,則會出現重大錯誤。我什至在我的查詢嘗試布爾檢查,它仍然給我錯誤
這裏是我得到的錯誤(我確定它從Occupation.typical_entry_level_education,因爲我已經刪除後者,它仍然給我錯誤)
education_levels[Occupation.typical_entry_level_education] <= education_levels[education])
KeyError: <sqlalchemy.orm.attributes.InstrumentedAttribute object at 0x3161e30>
試着看看這個:http://stackoverflow.com/questions/13582111/fetch-a-value-of-sqlalchemy-instrumentedattribute – dm295