Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get autoincremented value back after insert #10

Closed
omj opened this issue Jan 11, 2011 · 9 comments
Closed

Get autoincremented value back after insert #10

omj opened this issue Jan 11, 2011 · 9 comments

Comments

@omj
Copy link

omj commented Jan 11, 2011

If I want to insert values to table with autoincremented row, I want it's value back. How can I accomplish this with Scala Query?

@szeiger
Copy link
Member

szeiger commented Jan 11, 2011

Supporting this feature in ScalaQuery is challenging because different DBMSs require very different solutions (see http://en.wikipedia.org/wiki/Insert_%28SQL%29#Retrieving_the_key for an overview). Some of these solutions can be used easily on top of ScalaQuery (e.g. LAST_INSERT_ID() for MySQL) whereas others would need support from the ScalaQuery drivers (e.g. the RETURNING clause). In these cases, I suggest creating GUIDs on the client side or SELECTing incremented sequence values and using them for subsequent INSERTs as a work-around.

I am leaving this ticket open in the hope that it will be possible to define semantics for such a feature that can be made to work on most supported DBMSs.

@omj
Copy link
Author

omj commented Jan 12, 2011

Some of these solutions can be used easily on top of ScalaQuery

I'm using H2 where the function is :
INSERT INTO TABLENAME(COLUMNNAME1, COLUMNNAME2) VALUES(VAL1, VAL2);
SELECT SCOPE_IDENTITY()
So what should I do to insert values and retrieve id of inserted row?
Also,

SELECTing incremented sequence values

could be error prone for concurrent
environment so GUID is only possible workaround in such cases.

@szeiger
Copy link
Member

szeiger commented Jan 22, 2011

You can wrap add SCOPE_IDENTITY() as a ScalaQuery function:

val scopeIdentity = SimpleScalarFunction.nullary[Long]("scope_identity")

And then use it in queries:

 println("Inserted id "+Query(scopeIdentity).first)

Concurrent access is not a problem because SCOPE_IDENTITY() works independently for each session.

@macias
Copy link

macias commented Nov 29, 2011

I would vote even for partially implementing this, after all other features are also not equally implemented for all databases.

If I am not mistaken this feature is essential to fully support "clustered" inserts (master record + slave records).

@nafg
Copy link
Member

nafg commented Jan 6, 2012

@gsigmund
Copy link

Is there a plan when this will be implemented?

@nafg
Copy link
Member

nafg commented Mar 27, 2012

I think in SLICK (unless someone contributes it) -- I don't think Stefan is
putting more time into the current version.

On Mon, Mar 26, 2012 at 1:47 AM, gsigmund <
reply@reply.github.com

wrote:

Is there a plan when this will be implemented?


Reply to this email directly or view it on GitHub:
https://github.com/szeiger/scala-query/issues/10#issuecomment-4688263

@gsigmund
Copy link

ok. Is there info about SLICK available on the web?

@szeiger
Copy link
Member

szeiger commented Aug 9, 2012

Fixed in 09a65a8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants