[storm] Inserting a new object or updating an existing object
James Henstridge
james at jamesh.id.au
Wed Mar 3 07:50:10 GMT 2010
On Tue, Mar 2, 2010 at 4:08 PM, L.Guruprasad <lgp171188 at gmail.com> wrote:
> Hi all,
> I am new to using ORM and hence Storm as well. Here is my question.
>
> I have an object of a class A, named a1.
> I want to insert a1 into the database if there isn't already another
> record with the primary key value of a1.
> If there is a record already there, I want to update the record. How to
> go about doing this?
Assuming that you've got the Storm class defined something like:
class A(object):
__storm_table__ = 'a'
id = Int(primary=True)
...
You should be able to do something like:
obj = store.get(A, pk)
if obj is None:
obj = A()
obj.id = pk
store.add(obj)
# update other columns here.
store.commit()
This should result in a SELECT query followed by either an INSERT or UPDATE.
James.
More information about the storm
mailing list