Mismatch between the documentation and reality wrt recursive selects

28 views
Skip to first unread message

jonatha...@whatho.net

unread,
Aug 18, 2025, 4:53:31 PMAug 18
to py4web
A problem with something I'm trying to do led me to read about recursive selects in the documentation. I followed the example in the manual but it did not work. Trying to use recursive selects gave me Attribute Error.

Specifically:

```
db.define_table('person', Field('name'))

db.person.insert(name="Alice")
db.person.insert(name="Bob")
db.person.insert(name="Charlie")

db.define_table('thing',
                Field('name'),
                Field('owner_id', 'reference person'))

db.thing.insert(owner='Bob, name='Chair')
db.thing.insert(owner='Bob', name='Table')
db.thing.insert(owner='Alice', name='Table')

things = db(db.thing._id != None).select()
things = db(db.thing).select()
for thing in things:
    print(thing.name, thing.owner_id.name)

AttributeError: 'NoneType' object has no attribute 'name'
```

I'd be interested to know in what way I am misreading the relevant section of the manual.

Thank you very much,
Jonathan

Massimiliano

unread,
Aug 19, 2025, 3:24:02 AMAug 19
to jonatha...@whatho.net, py4web
I think should be:

db.define_table('person', Field('name'))

id_alice = db.person.insert(name="Alice")
id_bob = db.person.insert(name="Bob")
id_charlie = db.person.insert(name="Charlie")


db.define_table('thing',
                Field('name'),
                Field('owner_id', 'reference person'))

db.thing.insert(owner_id=id_bob, name='Chair')
db.thing.insert(owner_id=id_bob, name='Table')
db.thing.insert(owner_id=id_alice, name='Table')
db.commit()
things = db(db.thing).select()
print(things)

for thing in things:
    print(thing.name, thing.owner_id.name)
--
You received this message because you are subscribed to the Google Groups "py4web" group.
To unsubscribe from this group and stop receiving emails from it, send an email to py4web+un...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/py4web/ef509c9b-68b4-4bb5-a85b-ceac4dff509fn%40googlegroups.com.


--
Massimiliano

laundmo

unread,
Aug 20, 2025, 7:29:52 AMAug 20
to py4web
Some explanation:

You cant insert owner="Bob" because the db.thing table only has the fields "id", "name" and "owner_id". There could be 2 owners named "Bob", so py4web/pydal can't assume you meant the "Bob" you just inserted. It can only really insert the exact fields which you defined (and the automatic "id" field). Thats why you need to insert with "owner_id" and use the actual integer id of the correct owner.

Another note: The documentation shows these 2 lines, but only to show that they're equivalent:

things = db(db.thing._id != None).select()
things = db(db.thing).select() 

They both do the same thing, so you only need 1 of them.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages