.belongs problem

84 views
Skip to first unread message

Annet

unread,
Jun 26, 2016, 4:41:37 AM6/26/16
to web2py-users
 I have the following table definition

db.define_table('ntw_edge',
    Field('outID', 'reference vtx_vertex', default='', requires=[IS_IN_DB(db, 'vtx_vertex.id', '%(name)s')], ondelete='CASCADE', notnull=True, label='Out *', represent=lambda outID, row: db(db.vtx_vertex.id==outID).select().first().name if outID else ''),
    Field('inID', 'reference vtx_vertex', default='', requires=[IS_IN_DB(db, 'vtx_vertex.id', '%(name)s')], ondelete='CASCADE', notnull=True, label='In *', represent=lambda inID, row: db(db.vtx_vertex.id==inID).select().first().name if inID else ''),
    Field('label', length=128, default='', ondelete='RESTRICT', notnull=True, widget=SQLFORM.widgets.options.widget, label='Label *'),
    migrate = False)

In a function I have the following query:

vertices = db(db.ntw_edge.outID==1151).select(db.ntw_edge.inID, distinct=db.ntw_edge.inID)

events = db(db.event_list.vertexID.belongs(vertices)).select()


When I run the function I get an error

<type 'exceptions.TypeError'> long() argument must be a string or a number, not 'NoneType'



When I replace vertice with: (6, 28, 496, 1024)

events = db(db.event_list.vertexID.belongs((6, 28, 496, 1024)).select()

I get the right result.


So the problem is with:

vertices = db(db.ntw_edge.outID==1151).select(db.ntw_edge.inID, distinct=db.ntw_edge.inID)

Does it have anything to do with the valiadtor IS_IN_DB() or the represent? How do I solve
this issue?


Kind regards,

Annet

Annet

unread,
Jun 26, 2016, 4:56:32 AM6/26/16
to web2py-users
I just tried

vertices = db(db.ntw_edge.outID==1151).
select(db.ntw_edge.inID, distinct=db.ntw_edge.inID).as_list()

events = db(db.event_list.vertexID.
belongs(vertices)).select()

but that gives the following error:


<type 'exceptions.TypeError'> unhashable type: 'dict'



Kind regards,

Annet

Anthony

unread,
Jun 26, 2016, 7:41:11 PM6/26/16
to web2py-users
.belongs takes a list or tuple of values, but you have passed it a Rows object and a list of dictionaries.

Anyway, instead of using two queries, you can do it all in one with a nested select: http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#belongs.

Anthony

Annet

unread,
Jun 27, 2016, 2:00:30 AM6/27/16
to web2py-users
Hi Anthony,

Thanks for your reply.
._select() works.

I have not been able to figure out the 'one nested select' solution.

I have a table with network edges, I need the ids of the edges to which a
vertex connects. for example out = 1153 connects to in = (6, 28, 496, 1024)

Then I want to use in = (6, 28, 496, 1024) to select events from an event list

vertexID in table event_list does not reference table edge it references table
vertex as do the outID and inID in table edge.



By the way, in the book, shouldn't 'reference thing' be 'reference person'

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


Kind regards,

Annet

Anthony

unread,
Jun 27, 2016, 1:01:41 PM6/27/16
to web2py-users
On Monday, June 27, 2016 at 2:00:30 AM UTC-4, Annet wrote:
Hi Anthony,

Thanks for your reply.
._select() works.

I have not been able to figure out the 'one nested select' solution.

If you are using ._select(), then everything is happening in a single query rather than two queries (._select simply returns a SQL string, which then gets passed to .belongs).

Anthony
Reply all
Reply to author
Forward
0 new messages