On Apr 8, 6:48 pm, gwozdziu <[email protected]> wrote: > On Apr 8, 6:34 pm, Michael Bayer <[email protected]> wrote: > > > > > On Apr 8, 2011, at 10:54 AM, gwozdziu wrote: > > > > 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? > > > 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. > > 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.
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 received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
