Is there an easier way to initiate tables with references to currently undefined tables?

31 views
Skip to first unread message

Jordan Myers

unread,
Mar 2, 2017, 5:08:02 PM3/2/17
to web2py-users
My code has a lot of tables that reference other tables, some with circular dependencies. Whenever I deploy to a new environment, I always get "table does not exist" when trying to resolve a "reference table_x" Field type, so I comment out every Field that contains a reference to another table and then revert changes once all the tables get defined. This works but is very tedious. It is infeasible for me to move around the table definitions due to the circular definitions and my preference for maintaining organizational structure. 

Do people have an easier way to handle reference Field when deploying an application? If not, I am willing to contribute some code to accomplish this. Perhaps an option to the DB constructor where it creates the tables first but doesn't add any fields until later?

黄祥

unread,
Mar 2, 2017, 5:26:01 PM3/2/17
to web2py-users
just an idea why not define table in sequence number? 
first analyze the whole system and define table on order (define table first (low number) for the table with no relation to another table)
e.g.
models/db_schema_0_auth.py
auth.define_tables(username = True, signature = True)

models/db_schema_1_address.py
db.define_table('address', 
Field('address', 'text'), 
format = lambda r: '%s' % (r.address) )

models/db_schema_2_id_card.py
db.define_table('id_card', 
Field('id_card_number'), 
Field('first_name'), 
Field('last_name'), 
Field('address', 'reference address'), 
format = lambda r: '%s - %s %s' % (r.id_card_number, r.first_name, r.last_name) )

best regards,
stifan

Jordan Myers

unread,
Mar 2, 2017, 7:10:27 PM3/2/17
to web...@googlegroups.com
Thanks for the idea, Stifan! Unfortunately, I have circular dependencies (two tables reference each other), so this will not work for all tables. Also, I have over 100 tables and would like to keep them organized in files.

-Jordan


--
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 a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/vJEvUJ6_e-A/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kiran Subbaraman

unread,
Mar 2, 2017, 11:58:19 PM3/2/17
to web...@googlegroups.com
I don't have a solution for your situation, but this may help you devise a mechanism that works:
* circular-references in web2py: http://stackoverflow.com/a/3116032
* 'lazy_tables' : http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Lazy-Tables-a-major-performance-boost
________________________________________
Kiran Subbaraman
http://subbaraman.wordpress.com/about/
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.
Reply all
Reply to author
Forward
0 new messages