Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7495917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T18:23:22+00:00 2026-05-29T18:23:22+00:00

Web2py docs have two methods for inserting into a database db.tbl[0] = newRow and

  • 0

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?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T18:23:24+00:00Added an answer on May 29, 2026 at 6:23 pm

    A little bit of probing shows the difference between the two:

    For: db.tbl[0] = dict(name='something')

    File "/var/web2py/gluon/globals.py", line 172, in <lambda>
        self._caller = lambda f: f()
    File "/var/web2py/applications/myapp/controllers/default.py", line 114, in test
        db.tbl[0] = dict(name='something')
    File "/var/web2py/gluon/dal.py", line 5531, in __setitem__
        self.insert(**self._filter_fields(value))
    File "/var/web2py/gluon/dal.py", line 5605, in insert
        return self._db._adapter.insert(self,self._listify(fields))
    

    For: db.tbl.insert(name='something')

    File "/var/web2py/gluon/globals.py", line 172, in <lambda>
        self._caller = lambda f: f()
    File "/var/web2py/applications/myapp/controllers/default.py", line 115, in test
        db.tbl.insert(name='something')
    File "/var/web2py/gluon/dal.py", line 5605, in insert
        return self._db._adapter.insert(self,self._listify(fields))
    

    Both of them end up calling the same code to do the insert, so you will see that they run the same query:

    INSERT INTO tbl(name) VALUES ('something');
    

    Since the former does _filter_fields as 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] = newRow cannot return a value. You should just consider it a shorthand for insert and its cousin db.tbl[x>0] is extremely useful for updates and has the exact same notation and this helps simplify the code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Web2py has several methods for calculated fields, but the documentation states that lazy fields
I have users upload files into a fake directory structure using a database. I
I just found web2py a couple days ago, and have been reading the documentation
Im trying to get web2py running on an Ubuntu machine. All the docs seem
The web2py grid/smartgrid documentation documentation has a long list of parameters, but a sparse
I am writing a web2py application that requires summing dollar amounts without losing precision.
I am building an app using Web2py framework... I don't want to have to
In web2py, is there a way to have a piece of common code be
Now that I've gotten relatively familiar with web2py, I'd like to give Django a
I'm trying to make a query function that accepts two datetime.date object(start_date and end_date),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.