slick - Closing sequence of inserts (that return autoinc id) as a single DBIOAction -


tl;dr: want create pure function return sequence of insert operations closed inside single dbioaction object (e.g def foo(): dbioaction). trivial when ids not auto-incremented, gets difficult me when id's auto-incremented db).

i've been using slick more week , ability close sequence of operations dbioaction, can returned function , later can applied db side effect.

that worked fine when inserting data single table. how should deal situation when id of 1 inserted row must used in second row i'm inserting (like 1-many relation), given fact ids auto-incremented?

imagine have categories table columns: id, name, parent_id, parent_id either null or points other row within table.

if ids not auto-incremented, trivial:

def insert(cid: int, cname: string, subcid: int, subcname: string) = dbio.seq( categories +=(subcid, subcname, none), categories +=(cid, cname, some(subcid)) )

but when id auto-incrememnted, get's tricky. as stated in documentation, need use returning combined projection column needs returned, like:

val categoryid = (categories returning categories.map(_.id)) += (0, name, none)

now can use returned id when inserting second row. awesome. how can close sequences of db actions, since returned id, not action itself?

def insert(cname: string, subcname: string): dbioaction[int, stream, write] = ???

so answer own question might find weird @ first did.

val categoryid = (categories returning categories.map(_.id)) += (0, name, none)

trick categoryid dbioaction, not int expected be. trivial, there go.


Comments