Foreign keys in web2py database
Asked Answered
G

2

6

I use web2py framework with two tables. How can I refer one field from first table into second table?

Giuseppinagiustina answered 25/3, 2011 at 7:31 Comment(0)
F
8

Here's the section on table relationships and JOINs from the web2py chapter on databases

To illustrate how to implement one to many relations with the web2py DAL, define another table "dog" that refers to the table "person" which we redefine here:

>>> db.define_table('person',
                    Field('name'),
                    format='%(name)s')
>>> db.define_table('dog',
                    Field('name'),
                    Field('owner', db.person),
                    format='%(name)s')

You'll need to provide more information if you are looking for something that's not covered in that reference.

Fissure answered 25/3, 2011 at 13:24 Comment(0)
C
1

See The Database Abstraction Layer chapter of the book, in particular, the section on One to Many Relations. If you're working with a pre-existing/legacy database, see Legacy Databases and Keyed Tables. Also, here's an example.

Controller answered 25/3, 2011 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.