Web2py docs have two methods for inserting into a database
db.tbl[0] = newRow
and
db.tbl.insert(newRowAsDict)
The documentation implies that they are synonyms, but they appear to be different. For one, the insert method throws an exception if newRow contains fields that are not in the table. Also the .insert method returns the id of the added row, where the assignment doesn’t.
- Is this the intended behavior
- How can I get the id if I use the assignment method?
- Is the assignment method depricated?
A little bit of probing shows the difference between the two:
For:
db.tbl[0] = dict(name='something')For:
db.tbl.insert(name='something')Both of them end up calling the same code to do the insert, so you will see that they run the same query:
Since the former does
_filter_fieldsas is apparent from the trace, it does not thrown an exception when there are fields that are not present in the table, while the other does.Obviously,
db.tbl[0] = newRowcannot return a value. You should just consider it a shorthand for insert and its cousindb.tbl[x>0]is extremely useful for updates and has the exact same notation and this helps simplify the code.