Under what circumstances would db.define_table() return None

45 views
Skip to first unread message

Andrew Rogers

unread,
Jul 15, 2020, 2:45:36 AM7/15/20
to web2py-users
This code working fine but now it fails because the db.define_table() returns None.

table = db.define_table('DatabaseModel', Field('TableName'))  # This is not working now
for t in db.tables:
    table
.update_or_insert(TableName=t)

The only thing that I think I have changed that might have affected it was setting migrate = false in appconfig.ini. I have changed it back but to no avail. 

What could stop db.define_table from returning a table?

(I am using this to create an in-memory table which I populate with a list of actual tables.)

Thanks.

villas

unread,
Jul 15, 2020, 1:43:40 PM7/15/20
to web2py-users
You don't mention...
  • Which DB adapter you are using
  • Why just one of your tables is expected to be an in-memory table and the others not
  • Why you don't simply maintain the list of tables in a list(), or dict() or some other in-memory structure
In any case, maybe this idea might help...

db = DAL("sqlite://storage.sqlite") # or whichever DB you are using
dbmemo = DAL("sqlite:memory")

dbmemo.define_table('DatabaseModel', Field('TableName'))

for t in db.tables:
    dbmemo.DatabaseModel.update_or_insert(TableName=t)

Andrew Rogers

unread,
Jul 17, 2020, 3:05:40 AM7/17/20
to web2py-users
Thanks for your response Villas. My reason for this approach is that i have a lot of code that expects a table object and i am trying to avoid having to modify it to handle the a different format.

Anthony on Stack Exchange found my error. He said:
"You probably also set DAL(..., lazy_tables=True), in which case, db.define_table() returns None rather than a Table object, as the table is not actually created until first accessed via db.tablename.

Given your code, there is no benefit to setting lazy_tables=True, as all tables are accessed (and therefore created) immediately after definition anyway."


villas

unread,
Jul 17, 2020, 7:05:11 AM7/17/20
to web2py-users
That's great Andrew. 
BTW great to see that Anthony is helping - he is someone who really knows the nuts and bolts of web2py!

AGRogers

unread,
Jul 17, 2020, 10:54:05 AM7/17/20
to web...@googlegroups.com
That's for sure! His stack exchange posts really helped me get around some problems early on... and now :-) I should pray for a special blessing for him tomorrow 🙏 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/b3dcebaa-7aee-4e25-ab8f-fea7b28b86c5o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages