Search a Set in SQLAlchemy query

末鹿安然 提交于 2019-12-12 06:40:11

问题


I have 2 databases in completely different servers with no relationship between the two. There's one column in both databases that has identical data. I need to match based on that data so I can grab other info from both databases in one View.

Here's what I've got in my views.py

@view_config(route_name='cis', renderer='templates/cis.pt')
def cis(request):
    db1 = cis_db.query(site.site_id).join(site_tag).filter(site_tag.tag_id.like(202)).all()
    db2 = DBSession.query(A_School.cis_site_id).all()
    sites = set(db1).intersection(db2)
    return {
        'newsites': cis_db.query(site_tag).filter(site_tag.tag_id.like(202)).count(),
        'schools': DBSession.query(table).all(),
        'test': DBSession.query(table.cis_site_id.like(sites)).all(),
        }

My rendered page returns this error:

ProgrammingError: (ProgrammingError) ('Invalid parameter type.  param-index=0 param-type=set', 'HY105')

The sql code that follows has the correct numbers in it. I think the problem lies in what's returned in sites. Here's what the page returns immediately after that error:

u'SELECT [A_School].cis_site_id LIKE ? AS anon_1 
FROM [A_School]' (set([(1,), (2,), (3,), (4,), (5,)]),)

So the data returned looks correct but the leading set I think is throwing off the query that has sites in the .like section. Not sure how to get this working correctly.


回答1:


You should use in_(...) instead of like(sites)?
Actually, in_([s[0] for s in sites]) in order to unwrap the tuples from the sites set.



来源:https://stackoverflow.com/questions/21336914/search-a-set-in-sqlalchemy-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!