defining tree structure model problem

22 views
Skip to first unread message

Jurgis Pralgauskis

unread,
Mar 5, 2009, 5:01:14 PM3/5/09
to web...@googlegroups.com
Hello,

if I define like this:

db.define_table("TreeStructure",
SQLField("name", "string",notnull=True, default=None),
SQLField("parentID", db.TreeStructure))

I get
KeyError: 'TreeStructure' :/

how can I workaround?
I guess -- first create table, then alter it
but I am too busy to experiment and too used to plain sql:\

Thanks in advance :)

--
Jurgis Pralgauskis
Don't worry, be happy and make things better ;)
http://sagemath.visiems.lt

DenesL

unread,
Mar 5, 2009, 5:40:03 PM3/5/09
to web2py Web Framework
db.define_table("TreeStructure",
SQLField("name", "string",notnull=True, default=None),
SQLField("parentID", "integer"))

db.TreeStructure.parentID.type="reference TreeStructure"

mdipierro

unread,
Mar 5, 2009, 8:35:48 PM3/5/09
to web2py Web Framework
Your suggestions works but it will not be enforced at the database
level:

I suggest instead

db.define_table("TreeStructure",
SQLField("name", "string",notnull=True, default=None),
SQLField("parentID", "reference TreeStructure"))

Massimo

DenesL

unread,
Mar 6, 2009, 9:23:10 AM3/6/09
to web2py Web Framework
I see. But in a case like this:

db.define_table("customer",
SQLField("code","string"),
SQLField("name","string"),
SQLField("mainctc","integer"))
db.customer.mainctc.type='reference contact'

db.define_table("contact",
SQLField("number","integer"),
SQLField("customer",db.customer))

do I have to set the type as shown or is there a better way?.

mdipierro

unread,
Mar 6, 2009, 9:37:13 AM3/6/09
to web2py Web Framework
DO NOT DO THIS:

db.customer.mainctc.type='reference contact'

I cannot predict the downsides.

Massimo

DenesL

unread,
Mar 6, 2009, 10:03:57 AM3/6/09
to web2py Web Framework
OK, but then how would you define mainctc in the previous example?.

mdipierro

unread,
Mar 6, 2009, 10:24:15 AM3/6/09
to web2py Web Framework
There is not a way because I am not convinced this is a good thing to
do.
You can live the field as integer and use a IS_IN_DB validator to make
ure the int is a valid reference.

Massimo

Jurgis Pralgauskis

unread,
Mar 12, 2009, 5:40:15 AM3/12/09
to web...@googlegroups.com
by the way, to be able in db admin
to have dropdown reference to entered records (trunk/branches)
I need to have
db.TreeStructure.parentID.requires=IS_IN_DB(db, 'TreeStructure.id')


but that is problem if I am creating the first (root) element
(as workaround I commented out the IS_IN_DB line for this task,
or could add it in code at some init stage)

but would it be hard to make a drop-down binding, only when there are records
and leave integer box if there are none..?
I have web2py 1.56.4 (but didn't notice in158 changelog address this issue)
--
Jurgis Pralgauskis
tel: 8-616 77613;
jabber: jur...@akl.lt; skype: dz0rdzas;

mdipierro

unread,
Mar 12, 2009, 10:46:49 AM3/12/09
to web2py Web Framework
db.TreeStructure.parentID.requires=IS_NULL_OR(IS_IN_DB(db,
'TreeStructure.id'))

On Mar 12, 4:40 am, Jurgis Pralgauskis <jurgis.pralgaus...@gmail.com>
wrote:
Reply all
Reply to author
Forward
0 new messages