4

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" 

2 Answers 2

3

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)) 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply. But its not seems to be working.In query it is getting in as follows: ['1235b3a73ad24b9c86cf301525310b24'], ['cbdf25542c194a069464f69efff4859a'])\n"]
If I am using your first solution it is getting displayed as follows while I was printing a query: WHERE domains.tenant_id IN (:tenant_id_1, :tenant_id_2)) AS anon_1
Sorry, I didn't know structure of tenants, now second solution have to work too.
Thanks Dude. Second one is perfect.!:)
0

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)) 

2 Comments

But its not seems to be working.In query it is getting in as follows: ['1235b3a73ad24b9c86cf301525310b24'], ['cbdf25542c194a069464f69efff4859a'])\n"]
If I am following your solution value here is getting passed as a LIST. May be that is the issue here. As well as \n was also there, May be that one is an issue

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.