Fetching only one field using sorm framework

68 Views Asked by At

Is it possible to fetch only one field from the database using the SORM Framework?

What I want in plain SQL would be:

SELECT node_id FROM messages

I can't seem to be able to reproduce this in sorm. I know this might be against how sorm is supposed to work, but right now I have two huge tables with different kind of messages. I was asked to get all the unique node_ids from both tables.

I know I could just query both tables using sorm and parse through all the data but I would like to put the database to work. Obviously, this would be even better if one can get only unique node_ids in a single db call.

Right now with just querying everything and parsing it, it takes way too long.

1

There are 1 best solutions below

2
Nathaniel Ford On

There doesn't seem to be ORM support for what you want to do, unless node_id happens to be the primary key of your Message object:

val messages = Db.query[Message].fetchIds()

In this case you shouldn't need to worry about it being UNIQUE, since primary keys are by definition unique. Alternatively, you can run a custom SQL query:

Db.fetchWithSql[Message]("SELECT DISTINCT node_id FROM messages")

Note this latter might be typed wrong: you'd have to try it against your database. You might need fetchWithSql[Int], or some other variation: it is unclear what SORM does in the eventuality that the primary key hasn't been queried.