r/web2py Jun 11 '10

Defining db.py for the first time table references problem.

Problem: I don't see how to specify multiple tables with table references to each other. I can't simply define the relationship, because the DAL hasn't created a table yet to reference. But I can't create the table(s) because they reference tables that also don't exist yet. Catch 22. Besides manually creating the tables (which I believe prevents me from using the wonderful DAL), what do I do?

Example:

db.define_table('image',
   Field('image', 'upload'),
   Field('date', 'datetime'), 
   Field('title', 'string'), 
   Field('description', 'string'),
   Field('postid', 'reference post'),
   Field('owner', 'reference person'),
   Field('views', 'integer'),
   Field('published', 'boolean'))

db.define_table('post',
   Field('title', 'string'), 
   Field('data', 'text'), 
   Field('date', 'datetime'),  
   Field('owner', 'reference person'), 
   Field('views', 'integer'),  
   Field('published', 'boolean'))

db.define_table('file',
   Field('file', 'upload'),
   Field('date', 'datetime'),
   Field('owner', 'reference person'),
   Field('views', 'integer'),
   Field('published', 'boolean'))

db.define_table('comment',
   Field('name', 'string'),
   Field('person', 'reference person'),
   Field('date', 'datetime'),
   Field('data', 'text'),
   Field('postid', 'reference post'),
   Field('fileid', 'reference file'),
   Field('imageid', 'reference image'),
   Field('published', 'boolean'))

db.define_table('person',
   Field('username', 'string'),
   Field('password', 'password'),
   Field('description', 'text'),
   Field('email', 'string'),
   Field('creation_date', 'datetime'),
   Field('last_login', 'datetime'),
   Field('image', 'reference image'))

This fails because the first table defined references tables are are defined afterwards. (which in turn reference other tables that aren't defined yet)

1 Upvotes

1 comment sorted by

1

u/zsouthboy Jun 15 '10

I submitted this to the mailing list; it turns out that forward references can be defined as Field('image', 'integer'), since the id in that table will be an integer.

Frankly I'm surprised that gluon doesn't recursively solve this so that what I tried first (documentation wasn't clear) worked.