How to update multiple records

49 views
Skip to first unread message

Annet

unread,
Sep 25, 2016, 4:24:20 AM9/25/16
to web2py-users
In a table I defined a field:

Field('customLabel', length=128, requires=[IS_LENGTH(32), IS_NOT_EMPTY()]),

When the user updates this field I need the update to cascade to a field label
in another table:

Field('label', length=128, default='', requires=IS_IN_DB(db, 'ntw_edge_label_set.customLabel',
'%(customLabel)s', orderby='ntw_edge_label_set.customLabel')),

The problem is that it should not cascade to all the label fields containing the customLabels
just to the ones related to this specific user.

My first thought was to query the db

rows = db((table.outID==user_id) & (table.label==form.vars.customLabel)).select()
for row in rows:
    row.update_record(label=form.vars.customLabel)


I am wondering wether this is the most efficient way to update the records or whether
there is a better way to do this.


Kind regards,

Annet

Anthony

unread,
Sep 25, 2016, 2:00:10 PM9/25/16
to web2py-users
Once you have identified a Set of records, you can update directly without first selecting:

db((table.outID==user_id) &
   
(table.label==form.vars.customLabel)).update(label=form.vars.customLabel)

That will update all the records in one operation without reading anything from the database.

Anthony

Annet

unread,
Sep 27, 2016, 2:30:29 AM9/27/16
to web...@googlegroups.com
Hi Anthony,

Thanks for your reply. Update problem solved.

I have another problem related to this one, deleting a customLabel.


The table ntw_edge contains the field label:

Field('label', length=128, default='', requires=IS_IN_DB(db, 'ntw_edge_label_set.customLabel', '%(customLabel)s', orderby='ntw_edge_label_set.customLabel', zero=T("Select a value")), ondelete='RESTRICT', notnull=True),


The table 'ntw_edge_label_set' contains the field:


Field('customLabel', length=128, requires=[IS_LENGTH(32), IS_NOT_EMPTY()]),

Normally ondelete cascades the delete, so when I delete a customLabel all records in ntw_edge containing
that label will be deleted. However, in this case only the records with outID == user_id  should be deleted.

Am I right I have to delete the records from ntw_edge first and then delete the record from ntw_edge_label_set

db((table.outID==user_id) &
   (table.label==form.vars.customLabel)).update(label=form.vars.customLabel)
db(db.ntw_edge_label_set.id==int(request.vars.row)).delete()


Or is there a better way to do this?


Kind regards,

Annet



Best regards,

Annet
Reply all
Reply to author
Forward
0 new messages