0

Im trying to make a query that help me get a user by its username. What i want to do is to check if the given username is unique. so what i thought about is to uppercase the username when i do my select request and compare it with an uppercase of the given username. This is what i tried:

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

but it get this error:

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

I tried this too:

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

and i got this error:

[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]

What should i do ?

PS:Im using PostgreSQL 9.1

4

1 回答 1

2

首先,在 Play! 中,仅singleOpt在您期望正好返回一排时使用。否则会抛出异常。由于您确实只需要一行,LIMIT 1因此请在查询末尾添加 a。

你想要的 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 )

于 2012-09-21T10:31:30.440 回答