Is there any way to use PostgreSQL "Returning" in Insert queries with Anorm?

148 views
Skip to first unread message

Jonas

unread,
Jul 19, 2011, 10:34:36 AM7/19/11
to play-framework
Often when I use database tables, I have one column with an auto
incremented ID. When I do inserts to a table it is useful to know what
ID the row just inserted got. In PostgreSQL this can be returned on an
Insert query like: "INSERT INTO mytable .... RETURNING id", which is
useful. This can be used with JDBC but I haven't succeeded to use it
with Anorm.

If I use Anorms SQL("..") I can only use .executeUpdate() that returns
MayErr or an .execute() that returns an boolean. I tried to
use .executeQuery() but it doesn't exist.

Is there any way I can use PostgreSQL "Returning" with Anorm?

Thomas Dy

unread,
Jul 22, 2011, 2:24:10 AM7/22/11
to play-framework
From Anorm.scala, Magic's create function

val (statement, ok) = query.execute1(getGeneratedKeys=true)
val rs = statement.getGeneratedKeys();

It should then be the same when done via JDBC. I haven't tested
though.

BTW, how is your experience with Postgres+Anorm? I know mine isn't all
that pleasant

On Jul 19, 10:34 pm, Jonas <jlpetters...@gmail.com> wrote:
> Often when I use database tables, I have one column with an auto
> incremented ID. When I do inserts to a table it is useful to know what
> ID the row just inserted got. InPostgreSQLthis can be returned on an

Thomas Dy

unread,
Jul 22, 2011, 2:19:26 AM7/22/11
to play-framework
From the Anorm source, in the create function there's:

val (statement, ok) = query.execute1(getGeneratedKeys=true)
val rs = statement.getGeneratedKeys();

I think that's what you're looking for?

BTW, how are you faring with Anorm+Postgres. I've run into quite a few
problems with it already, particularly with getting Magic to work.

On Jul 19, 10:34 pm, Jonas <jlpetters...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages