Murali
Murali

Reputation: 1114

Passing an str to SQL alchemy Query

Actually I have a code which is fetching necessary details from a text file.

Here I am fetching the some id from text file.

Then I need to pass the same in to SQLAlchemy query to fetch the results.

But I am no t getting the results as needed here.

here it is the code:

addys = ','.join('"{}"'.format(''.join(tenant)) for tenant in tenants if tenant)
#print "%s" % (addys)

# cleanup and close files
files.close()

query1 = query1.filter(model.name == "in-addr.arpa.")
query2 = query2.filter(model.tenant_id.in_([addys]))

Here type of 'addys' is as follows:

<type 'str'>

I am not getting the result here as needed.

Some one help me with the same.

Note:

While printing the addys getting the results as follows which is obviously correct:

"1235b3a73ad24b9c86cf301525310b24","cbdf25542c194a069464f69efff4859a"

Upvotes: 4

Views: 415

Answers (2)

Eugene Soldatov
Eugene Soldatov

Reputation: 10135

Argument for _in method should be an array of ids, but you pass to it list with one element - string with all id, divided by commas. You need split string with ids and pass them as list:

query = query.filter(model.tenant_id.in_(addys.split(',')))

Or change your previous lines:

tenant_ids = [''.join(tenant) for tenant in tenants if tenant]
query = query.filter(model.tenant_id.in_(tenant_ids))

Upvotes: 3

matino
matino

Reputation: 17715

You don't have to join the strings, it's enough to:

addys = [tenant for tenant in tenants if tenant]

and

query2 = query2.filter(model.tenant_id.in_(addys))

Upvotes: 0

Related Questions