2012-09-21 28 views
0

我試圖做一個查詢,幫助我通過用戶名獲取用戶。我想要做的是檢查給定的用戶名是否是唯一的。所以我想到的是當我執行我的選擇請求時用大寫的用戶名,並將它與給定用戶名的大寫字母進行比較。這是我的嘗試:Playframework SQL查詢

SQL("select * from utilisateur where upper(pseudo) = {pseudo}").on(
     'pseudo -> pseudo.toUpperCase 
     ).as(Utilisateur.utilisateur.singleOpt) 

但得到這個錯誤:

[RuntimeException: SqlMappingError(too many rows when expecting a single one)] 

我想這太:

SQL("select * from utilisateur where ucase(pseudo) = {pseudo}").on(
     'pseudo -> pseudo.toUpperCase 
     ).as(Utilisateur.utilisateur.singleOpt) 

,我得到這個錯誤:

[PSQLException: ERROR: function ucase(character varying) does not exist Indice : No function matches the given name and argument types. You might need to add explicit type casts. Position : 33] 

我該怎麼辦?

PS:IM使用PostgreSQL 9.1

回答

2

首先,在玩!只使用singleOpt如果你希望只有一行回來。否則,它會拋出異常。由於您只需要一行,請在您的查詢結尾處添加一個LIMIT 1

你想要的PostgreSQL功能是upper()。這裏有一個例子:

SQL("select * from utilisateur where upper(pseudo) = {pseudo} limit 1").on(
     'pseudo -> pseudo.toUpperCase 
     ).as(Utilisateur.utilisateur.singleOpt) 

(你可以用它在這裏玩:http://sqlfiddle.com/#!1/6226c/5

+0

感謝您對這個工作me.As罰你所說的問題是從'singleOpt'來,因爲它越來越多行當它只檢查一個。 –