`update_or_insert()` is always inserting?

101 views
Skip to first unread message

Alec Taylor

unread,
Jul 31, 2012, 11:10:36 AM7/31/12
to web...@googlegroups.com
I want only one value stored per unique user, but using the `update_or_insert()` function is storing multiple values:

db.define_table(
    'foo',
    Field('haz', notnull=True, unique=True, requires=IS_IN_SET(['a', 'b', 'c'])),
    Field('user_id', db.auth_user, unique=True)
)

db.foo.update_or_insert(haz='a', user_id=1)
db.foo.update_or_insert(haz='b', user_id=1)
db.foo.update_or_insert(haz='c', user_id=1)

It should only have one record at the end of this, with haz='c' and user_id=1. Instead, it provides 3 records.

How do I get this to work properly?

Thanks for all suggestions,

Alec Taylor

Cliff Kachinske

unread,
Jul 31, 2012, 11:21:42 AM7/31/12
to web...@googlegroups.com
You need the other form of update_or_insert:.  This is covered in the manual.  I always encourage people to Read The Fine Manual (RTFM) to gain a good understanding what's going on.

 
db.person.update_or_insert(db.person.name=='John',
 name
='John',birthplace='Chicago')

or

db.foo.update_or_insert(db.foo.user_id==1, haz='c', user_id=1)

Massimo Di Pierro

unread,
Jul 31, 2012, 11:49:01 AM7/31/12
to web...@googlegroups.com
What you have is correct in the sense that you want to do something that update_or_insert cannot do. As I look at it I realize update_or_insert is a little useless as implemented. I just changed it in trunk.

Get the latest trunk and try this:

db.foo.update_or_insert({'user_id':1},haz='a')

this updates haz if a record with user_id==1 exists.

Alec Taylor

unread,
Jul 31, 2012, 11:51:13 AM7/31/12
to web...@googlegroups.com
Thanks, that did the trick.

--
 
 
 

Reply all
Reply to author
Forward
0 new messages