db is not defined in other model files

51 views
Skip to first unread message

Vic Ding

unread,
May 4, 2016, 11:56:07 AM5/4/16
to web2py-users
Hi all,

I have a weird problem with web2py on apache. I created db1.py and created defined tables in it and it works fine. I created another model, name is my_list.py and it does not work. Error is db is not defined in my_list.py. Any idea what the problem might be?
Thanks!

Anthony

unread,
May 4, 2016, 12:09:29 PM5/4/16
to web2py-users
Does it work locally?

Vic Ding

unread,
May 5, 2016, 9:20:50 AM5/5/16
to web2py-users
The issue is gone magically. This happened before, the same issue come and go. 
I tried to add/edit db files both locally (from web UI) and externally (PyCharm on my laptop through SSH sync), both worked fine. 

One other issue is I have db.py, db1.py and test_list.py as model files. In test_list.py I defined a table which is referenced in db1.py. 
db1.py
db.define_table('customer',
                auth
.signature,
               
Field('name','string',requires=IS_NOT_EMPTY()),
               
Field('address1','string',requires=IS_NOT_EMPTY()),                
               
format='%(name)s'
                )
test_list.py
db.define_table('country_list2',
               
Field('name'),
               
Field('test','reference customer')
               
)

I get a complain that "Cannot resolve reference customer in country_list2 definition".  Is there a better way to organise the model files? Does this mean that the model files are loaded in alphabetical order? 

Thanks in advance 

Vic

Mirek Zvolský

unread,
May 5, 2016, 9:52:17 AM5/5/16
to web2py-users
alphabetical order ?
Will
print 'db.py' < 'my_list.py'
print True in that environment?

And I don't know what web2py uses for alphabetical sorting of models.

Try add
print "db" and print "my_list"
into that models
to see if "my_list" really starts later.

If it is well,
cannot be db variable deleted somewhere?



Dne čtvrtek 5. května 2016 15:20:50 UTC+2 Vic Ding napsal(a):

Mirek Zvolský

unread,
May 5, 2016, 10:00:02 AM5/5/16
to web2py-users
Models run in alphabetical order.

You should earlier define tables with primary keys for joins,
and later tables with foreign keys of such joins.

However if this is not possible,
there is an alternative syntax for foreign keys which will help:
Use
'reference joinedtable'
instead of
db.joinedtable

See web2py.com/book, chapter 6, "Self-Reference and aliases" for details.




Dne čtvrtek 5. května 2016 15:20:50 UTC+2 Vic Ding napsal(a):
The issue is gone magically. This happened before, the same issue come and go. 

Mirek Zvolský

unread,
May 5, 2016, 10:04:14 AM5/5/16
to web2py-users
I'm sorry,
I read to late that you already use 'reference...'
So back to previous:
Add some printing or logging to see the order how the models really fire.



Dne čtvrtek 5. května 2016 16:00:02 UTC+2 Mirek Zvolský napsal(a):

Vic Ding

unread,
May 5, 2016, 10:04:36 AM5/5/16
to web2py-users
Hi Mirek,
Thanks for your kind help. I will look again into the book. 
Reply all
Reply to author
Forward
0 new messages