There are two formats for declaring a reference: .... db.Field('owner', db.other_table) # this must have other_table already defined, or it is a Python error; ... db.Field('owner', 'reference other_table') # this is delayed evaluation, and what you would use to have two tables cross link each other;
On Tue, Jul 21, 2009 at 10:27 PM, JoshuaJ <surely.you.j...@gmail.com> wrote: > > It seems that in web2py, if one table is to have a foreign key to > another table, the target table must be defined first in db.py. Does > this mean that there is no way to have two tables that link to each > other? > > Specifically: I'm writing a basic multi-user address book application. > Each user, of course, owns multiple contacts. However, each user > selects at most one of their contacts as being their identity, the > contact that represents themselves. > > The way I'd do this in a traditional database would be to define a > "users" table with fields "username" (a string) and > "primary_contact_id" (foreign key into contacts table); and then a > "contacts" table with fields "contact_data" (i'm simplifying here) and > "owner_id" (pointing back into the users table). However, Web2py > doesn't seem to allow this. > > Does anyone have a solution? > > (One workaround would be to add an "is_identity" field to each > contact, but this seems like a nasty kludge, firstly because since it > will have the value of "no" for the vast majority of contacts, but > more importantly because it doesn't guarantee data integrity -- a bug > in the code could leave a user with multiple identities, something > impossible with the other representation.) > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to web2py@googlegroups.com To unsubscribe from this group, send email to web2py+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---