Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
saving existing db object
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
gwozdziu  
View profile  
 More options Apr 8 2011, 10:54 am
From: gwozdziu <sz.gwo...@gmail.com>
Date: Fri, 8 Apr 2011 07:54:37 -0700 (PDT)
Local: Fri, Apr 8 2011 10:54 am
Subject: saving existing db object
Hi!

In SqlAlchemy 0.4 when I tried to save (using method "save") to
database object which interferes row which already exists in database
(for example I have table with primary key `id`, row with id = 3 in
this table, and I am trying to save object with the same id = 3) the
operation raises error that object is already persistent.

In Sqlalchemy 0.5.7 there is no "save" method, but there is "add"
method. But when I'm trying to save (using "add" method) object which
interferes row which is already in database - operation is successful.
But I need to have some error raised in that situation, or maybe other
solution that informs me that I'm not actually inserting object but
updating.

What method can I use? I read documentation and I didn't found
solution.

Am I missing something?

Szymon


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Bayer  
View profile  
 More options Apr 8 2011, 12:34 pm
From: Michael Bayer <mike...@zzzcomputing.com>
Date: Fri, 8 Apr 2011 12:34:56 -0400
Local: Fri, Apr 8 2011 12:34 pm
Subject: Re: [sqlalchemy] saving existing db object

On Apr 8, 2011, at 10:54 AM, gwozdziu wrote:

you should be getting an IntegrityError raised if you add() a new object to the session that has a primary key which is the same as one already in the database.   add() is the same as the former save_or_update() method, which would call straight down to save() if the object were pending.

When you refer to needing to know if you're inserting or updating, im not clear on what the issue is - if you have an object that is "detached", meaning it has a row in the DB, you add() it to a session, the session already has an object with that identity, you get an error.    So I'm not really sure what the specific problem is here.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
gwozdziu  
View profile  
 More options Apr 8 2011, 12:48 pm
From: gwozdziu <sz.gwo...@gmail.com>
Date: Fri, 8 Apr 2011 09:48:49 -0700 (PDT)
Local: Fri, Apr 8 2011 12:48 pm
Subject: Re: saving existing db object
On Apr 8, 6:34 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:

1. I'm not getting IntergrityError. You said that add is the same as
save_or_update() method, but I'm interested in method which is the
same as save() method - this method raises IntegrityError when I am
trying to add new object to the the session that has a primary key
which is the same as one already in the database.
2. My issue is that I'm creating REST API on my database. When
somebody is trying to create object with existing id (there is an
object in db with this id) I don't want to allow him to update this
object - I want to reject his request.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
gwozdziu  
View profile  
 More options Apr 8 2011, 12:59 pm
From: gwozdziu <sz.gwo...@gmail.com>
Date: Fri, 8 Apr 2011 09:59:38 -0700 (PDT)
Local: Fri, Apr 8 2011 12:59 pm
Subject: Re: saving existing db object
On Apr 8, 6:48 pm, gwozdziu <sz.gwo...@gmail.com> wrote:

here is the piece of code:

#before executing this code we have object with id = 5 in our database

obj = self.mapper.get_from_dto(dto)  # creating the object from values
provided by user , this object has id = 5
self.mapper.add(obj) # we are putting new object (with id = 5). It
conflicts with already existing object
self.mapper.commit() # commit. New object overrides old one

This is not what I'm expecting. I don't want to overriding (updating)
old object (row).


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Bayer  
View profile  
 More options Apr 8 2011, 1:42 pm
From: Michael Bayer <mike...@zzzcomputing.com>
Date: Fri, 8 Apr 2011 13:42:24 -0400
Local: Fri, Apr 8 2011 1:42 pm
Subject: Re: [sqlalchemy] Re: saving existing db object

On Apr 8, 2011, at 12:59 PM, gwozdziu wrote:

> #before executing this code we have object with id = 5 in our database

> obj = self.mapper.get_from_dto(dto)  # creating the object from values
> provided by user , this object has id = 5
> self.mapper.add(obj) # we are putting new object (with id = 5).
> It
> conflicts with already existing object
> self.mapper.commit() # commit. New object overrides old one

this all depends on what get_from_dto() does.  If it says:

        obj = MyObject(<values from the user>)

now "obj" is known as "transient".  When you add() it to the session, it becomes "pending".  When flush() proceeds, you will get an Integrity Error if ID # 5 already exists.   If you want to merge the values from MyObject onto a possibly already existing object in the DB, you use merge:   obj = session.merge(obj).

On the other hand, if get_from_dto() says:

        obj = session.query(MyObject).get(5)

then "obj" is "persistent", and is already in the session.   Calling session.add(obj) has no net change.   There's no error but also nothing has actually happened.

Finally, if get_from_dto() is more like:

        obj = some_other_session.query(MyObject).get(5)
        some_other_session.close()

then "obj" is detached.    When you call session.add(obj), "obj" becomes the object that represents row #5 in the session.    This might be what you're looking for, but this is an unusual case.   There is of course no error because you are handing the Session an object that is detached, and becomes persistent again - it represents row #5 from the database already, so its the correct row, though it may be out of date.   If you've done something to it that you don't want to persist, that's more on your end, but it's a little strange your application isn't able to be aware of that much sooner.

Anyway, you can test for "identity" using:

        from sqlalchemy.orm.util import has_identity
        if not has_identity(obj):
                session.add(obj)

if the case is as simple as, the row is out of date and you want to guard against that, you can use a version_id_col for that use case - it will raise if the version you give to the session is older than what's currently in the database.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
gwozdziu  
View profile  
 More options Apr 13 2011, 4:40 am
From: gwozdziu <sz.gwo...@gmail.com>
Date: Wed, 13 Apr 2011 01:40:32 -0700 (PDT)
Local: Wed, Apr 13 2011 4:40 am
Subject: Re: saving existing db object
Thanks Michael! I've got a small bug in my get_from_dto - I was
getting object from db, and that was reason the object was updating.

On Apr 8, 7:42 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »