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

Questions about "IsNew" and "Save" #10

Closed
mmoore99 opened this issue Apr 22, 2011 · 6 comments
Closed

Questions about "IsNew" and "Save" #10

mmoore99 opened this issue Apr 22, 2011 · 6 comments

Comments

@mmoore99
Copy link

Am I correct to assume that "IsNew" and "Save" will only work properly if the database is using an auto generating primary key?

If the the database is using a "natural" primary key like an invoice number that is being created by the program then that property will be populated by the program when the object is created and the IsNew function will return false (because the primary key is populated) even though the record is not already in the database. Therefore, "Save" will not work properly.

Am I understandingt that correctly? If so, are there any plans to have these functions work with "natural" primary keys?

@toptensoftware
Copy link
Collaborator

You're right... PetaPoco assumes primary keys are populated by the DB... I've been meaning to address this but just haven't yet.

@mmoore99
Copy link
Author

Thanks for the quick response. These kinds of issues are easy enough to work around when you are aware of them. I think it might be worthwhile to document these kinds of assumptions so people don't get caught by surprise.

I think you have done a great job with PetoPoco and look forward to following its progress.

@toptensoftware
Copy link
Collaborator

I've just committed fixes to support for non-auto-increment/non-identity primary key columns. Insert() should now work correctly, but IsNew() and Save() will not.... there's simply no way for PetaPoco to know. In GitHub only atm... will include in next NuGet package.

@mmoore99
Copy link
Author

Thanks for the quick response...very impressive. Keep up the good work!

On Fri, Apr 22, 2011 at 7:34 AM, toptensoftware <
reply@reply.github.com>wrote:

I've just committed fixes to support for non-auto-increment/non-identity
primary key columns. Insert() should now work correctly, but IsNew() and
Save() will not.... there's simply no way for PetaPoco to know. In GitHub
only atm... will include in next NuGet package.

Reply to this email directly or view it on GitHub:
#10 (comment)

@toptensoftware
Copy link
Collaborator

@matthewsheeran
Copy link

Yep I hit this one too and had to write an alternate Save() that checks an Exists() on the PK value before deciding to do the update or insert. Mind you my PK is a VARCHAR and not an AUTO INCREMENT so methinks that you must be caching the largest autoincrement which is why IsNew() does sweet FA i.e. doesn't query the database that I can see BUT now I do entirely understand why! ;-)

And yes Insert and Update work as long as I don't insert a false for AutoIncrement in the parameter list for the former.

Upsert() anyone that does what I am doing above with Exists on a specified PK?

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

3 participants