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

Let upsert return the ID of the row added / updated. #43

Closed
brutus opened this issue Nov 18, 2013 · 2 comments
Closed

Let upsert return the ID of the row added / updated. #43

brutus opened this issue Nov 18, 2013 · 2 comments

Comments

@brutus
Copy link

brutus commented Nov 18, 2013

Just started playing around with dataset and was thinking this would be nice. I keep writing small insert / update wrappers right now.

@pudo
Copy link
Owner

pudo commented Nov 18, 2013

Cool idea. The problem with this is that upsert can update multiple rows at once if the update criteria are open enough. So there would need to be handling for that case. Still, want to give it a shot?

@brutus
Copy link
Author

brutus commented Nov 18, 2013

I just give it a very quick look. Seems that:

  • INSERT returns the PK of the row added.
  • UPDATE returns True if any rows where updated, else False.
  • UPSERT returns nothing.

So a consistent solution might be to return the ID on INSERT or a single UPDATE, True on multiple updates and False otherwise (on error). This also avoids the problem of having to guess if the return value is a ID or the numbers of rows changed.

As a side note:

try:
  # update code
  return rp.rowcount > 0
except KeyError:
  return False

Why not:

try:
  # update code
  return rp.rowcount
except KeyError:
  return 0

It seems to me we just loose information on the 1st version.
And the 2nd version still works fine in a simple if check.

But in the upsert function the casting done in the 1st version would be necessary.

This could look kinda like this: brutus@044dcce
Just a quick try in the on site editor. Couldn't test it from here.

@pudo pudo closed this as completed in 5375572 May 21, 2015
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

2 participants