yes - my point is this is not obvious, so effecting the same by adding the syntax to accomplish the same would be [a] more obvious, and can be talked about in the appropriate section of the manual, and [b] make the code more readable, e.g.;
On Sun, Sep 6, 2009 at 10:07 AM, mdipierro <[email protected]> wrote: > > ERRATA: > > I do not know about Django but in web2py you can do: > > db.define_table('student',Field('name')) > db.define_table('faculty',Field('name')) > # if we could replace this kind of code: > # db.define_table('user',Field('type'),Field('ref_id','integer')) > # with something like this, # it would be self-documenting, and make code more readable, maintainable: db.define_table('user',Fiel('type'),Field('ref_id','reference generic')) > > id1=db.student.insert(name="Max") > db.user.insert(type="student",ref_id=id1) > id2=db.faculty.insert(name="John") > db.user.insert(type="faculty",ref_id=id2) > Anyway - it would be no more than syntactic sugar - do people think this is worth the trouble to have? > > for row in db().select(db.user.ALL): > print row.id, row.type, db[row.type][row.ref_id].name > > outputs: > 1, student, Max > 2, faculty, John > > Massimo > > On Sep 6, 9:26 am, mdipierro <[email protected]> wrote: > > I do not know about Django but in web2py you can do: > > > > db.define_table('student',Field('name')) > > db.define_table('faculty',Field('name')) > > db.define_table('user',Field('type'),Field('ref_id')) > > > > id1=db.student.insert(name="Max") > > db.user.insert(type="student",ref_id=id1) > > id2=db.faculty.insert(name="John") > > db.user.insert(type="faculty",ref_id=id2) > > > > for row in db().select(db.user.ALL): > > print row.id, row.type, db[row.type][row.ref_id].name > > > > outputs: > > 1, student, Max > > 2, faculty, John > > > > Massimo > > > > On Sep 6, 12:40 am, Yarko Tymciurak <[email protected]> wrote: > > > > > correct -- there currently is no "reference generic" (and integer is > the > > > way); > > > > > My question was: would it serve the community, be more obvious is > there > > > _was_ a > > > "reference generic" so that these questions wouldn't need to come > up.... > > > > > On Sun, Sep 6, 2009 at 12:00 AM, mdipierro <[email protected]> > wrote: > > > > > > Instead of 'reference generic' just use 'integer' and everything else > > > > will work. > > > > > > On Sep 5, 10:27 pm, Yarko Tymciurak <[email protected]> wrote: > > > > > On Sat, Sep 5, 2009 at 9:40 PM, Matthew < > [email protected]> > > > > wrote: > > > > > > > > Does Web2py have any concept of Polymorphic Associations like > Ruby or > > > > > > Generic Relations like Django? > > > > > > > I think this is simple - rather than declaring a Field as a > db.table > > > > (ergo a > > > > > reference), or a forward "reference" to another table, you can > simply > > > > define > > > > > the field as was done before the forward reference syntax - as an > integer > > > > > field. > > > > > > > We could make a generic reference to affect this - perhaps it would > be > > > > > easier for people to remember, e.g.: > > > > > > > db.table_define( 'mytable', > > > > > # .... > > > > > Field( 'myref', 'reference generic' ); # DAL doesn't have > this; > > > > > should it so that generic references are explicitly named? > > > > > > > If this seems too simple, remember the DAL makes a default ID, and > it is > > > > > numeric and autoincrementing (there are no classes to deal with). > > > > > > > No you can simple do something like > > > > > > > for for in db(db.othertable.id == db.mytable.myref).select(): > > > > > print row.value_of_interest > > > > > > > So - there are your "Polymorphic Associations" or "Generic > Relations" --- > > > > > without the fancy names or complications - the ID is just an index > - an > > > > > integer, and uniformly so. > > > > > > > - Yarko > > > > > > > > This would be very beneficial for tagging or voting, because > there is > > > > > > usually a need to tag or vote on different models in the same > > > > > > application. > > > > > > > > Thanks, > > > > > > Matthew > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "web2py-users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/web2py?hl=en -~----------~----~----~----~------~----~------~--~---

