db.define_table('abc', Field('name'))
db.define_table('cde', Field('name2', 'reference 'abc'))
db.define_table('abc', Field('name'), Field('guesswhat', 'reference cde'))
db.define_table('cde', Field('name2', 'reference 'abc'))
db.define_table('abc', Field('name'), Field('guesswhat', 'reference cde'))
db.define_table('cde', Field('name2', 'reference 'abc'))
--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
1) Fix the doc, stop pretend that 'reference tablename' will create FK for you in every situation (I can fix the doc about that and make a PR on the book)Hello,Funny because, this morning I actually fix a answer on SO where the model was not define using 'reference tablename'...Everything is in Simone email... If it can be fixed rapidly, just do it as Paolo suggest... But if there is more consideration to take inton account, here what I would do...
Something like : It is prefered to use Field('fieldname', 'reference tablename') instead of Field('fieldname', db.tablename) because it solves alaphabetical blabla model definition... But in some situation you may be fooled to think that backend FK have been enforced, because if a refered table is not define at the moment your model is first migrated web2py won't be able to add the FK later... Blabla... Then the workaround that at least prevent an null input and a wrong value "IS_IN_DB(...)"... Advised developper should make sure his FK are actually defined checking at backend table definition....
--
reference <table> | IS_IN_DB(db,table.field,format) |
To illustrate how to implement one to many relations with the web2py DAL, define another table "thing" that refers to the table "person" which we redefine here:
>>> db.define_table('person',
Field('name'),
format='%(name)s')
>>> db.define_table('thing',
Field('name'),
Field('owner_id', 'reference person'),
format='%(name)s')
Table "thing" has two fields, the name of the thing and the owner of the thing. The "owner_id" field id a reference field. A reference type can be specified in two equivalent ways:
Field('owner_id', 'reference person')
Field('owner_id', db.person)
The latter is always converted to the former. They are equivalent except in the case of lazy tables, self references or other types of cyclic references where the former notation is the only allowed notation.
When a field type is another table, it is intended that the field reference the other table by its id. In fact, you can print the actual type value and get:
>>> print db.thing.owner_id.type
reference person
Now, insert three things, two owned by Alex and one by Bob:
>>> db.thing.insert(name='Boat', owner_id=1)
1
>>> db.thing.insert(name='Chair', owner_id=1)
2
>>> db.thing.insert(name='Shoes', owner_id=2)
3
You can select as you did for any other table:
>>> for row in db(db.thing.owner_id==1).select():
print row.name
Boat
Chair
Because a thing has a reference to a person, a person can have many things, so a record of table person now acquires a new attribute thing, which is a Set, that defines the things of that person. This allows looping over all persons and fetching their things easily:
>>> for person in db().select(db.person.ALL):
print person.name
for thing in person.thing.select():
print ' ', thing.name
Alex
Boat
Chair
Bob
Shoes
Carl
There is no mention of the possible issues you describe... Nor which issue reference is intent to resolve (python alphanumeric module load and models order in models files)... It used to be explained before if I recall...
So why :
They are equivalent except in the case of lazy tables, self references or other types of cyclic references where the former notation is the only allowed notation.ok, I'll rephrase: is there anything in the docs that states something wrong or you just want to add something ?
--
I don't see any errors on it. Show what you'd like to put in it so we can comment.
--