0

I'm trying to add(or update in case it isn't alreadythere) some records to a db in a loop and I want to fill the entries with the sum of the default db values with some other values, but somehow, when trying to insert,it's not working. It's something like this:

                    db.cTable.update_or_insert((db.cTable.User == row.id)  & (db.cTable.Year == int(datatemp[2])) & 
           (db.cTable.Month == int(datatemp[1])),User = row.id, 
            Year = int(datatemp[2]), Month = int(datatemp[1]), 
            cost = db.cTable.cost + data[t][0])

Any ideas?

Carla Raquel
  • 115
  • 3

1 Answers1

0

db.cTable.cost is a Field object, not a value. If you want to add its default value, you must access its default property:

db.cTable.update_or_insert(..., cost = db.cTable.cost.default + data[t][0])
Anthony
  • 25,466
  • 3
  • 28
  • 57
  • What if I want to add the field's current value(instead of default) to data[t][0] ? – Carla Raquel Oct 21 '15 at 16:06
  • I tried using db.cTable.cost + data[t][0] for the above but it doesn't work with the update_or_insert function. – Carla Raquel Oct 21 '15 at 16:13
  • Adding the current value makes sense only for an update, so obviously wouldn't work when doing an insert. If you need to insert different values depending on whether it is an insert or an update, you'll probably have to explicitly code the logic rather than using the `update_or_insert` method. – Anthony Oct 21 '15 at 16:42