Issue: peewee regexp works only the first time
Author: ghost
State: closed
Created: 2014-08-14 21:50:49

I'm running into an issue with the peewee regexp function. Queries like this work the first time I run it:

query = Table.select().where(Table.column.regexp('string'))

However, if I try to run it with a different string, I get this error: 'unicode' object has no attribute 'regexp':

query = Table.select().where(Table.column.regexp('string2'))

I usually run it twice because I need to see how specific to make the regexp string.

I've tried disconnecting/reconnecting to the db, but that doesn't help. The only thing that works is if I close ipython and restart it. Is this a memory/buffer issue? Is there a good way to get this to work? Thanks in advance.


The `regexp` function is covered by unit tests and I can verify from local testing that it works. Can you share your code that is causing the problem? I wonder if at some point in between you are assigning a value to `Table.column` (maybe using single `=` sign instead of 2)?


Hello. This is usually what I run:
query = Table.select().where(Table.column.regexp('string'))

I've also tried doing to see if I need to execute the query at least once to "release" it from memory -
for column in Table.select().where(Table.column.regexp('string')):
    print Table.column

Neither of those seems to work once I've done this the first time (by either method). Somehow it seems to be converting Table.column to unicode so that the next time I run it, it thinks I'm calling .regexp() on the unicode and not on the Table.column object. How do I release it so that it thinks of Table.column as the database object rather than whatever it pulled the first time?

Thanks!


> Somehow it seems to be converting Table.column to unicode so that the next time I run it, it thinks I'm calling .regexp() on the unicode and not on the Table.column object.

Right, which is why I asked if you were assigning a value to `Table.column` elsewhere. Can you share some code that I can use to try and reproduce the issue? Or share the actual code you're running? Also, what database engine are you using?


Oh I see - sorry.

I don't assign a value to Table.column anywhere... I literally just try to call this twice in a row.

My code is really this simple, actually... I just started using peewee last night. :)

The flow is as follows:
query = Table.select().where(Table.column.regexp('string'))
query = Table.select().where(Table.column.regexp('string2'))

or 
query = Table.select().where(Table.column.regexp('string'))
for column in Table.select().where(Table.column.regexp('string2')):
    print Table.column

The second of each results in the unicode error above.

The database itself is a MySQL db.

(I'm about to go catch a plane - will check back as I find wifi. Thx in advance!!)


Hmm, I'm confused why you're doing the first query if you don't do anything with the result. Is this literally the exact code you're using? If not I'd appreciate you sharing some code to reproduce the bug.


I do use the result... I look to see what it returns and then fine tune my regex. I don't know how specific of a lookup I want to use, so I have to try it a few times. 


Since the consecutive queries with regexp haven't been working, I've been using MySQL for queries and using peewee just for data pulls. I guess I could go straight from MySQL as well to cut out some extra steps.


Sorry you had trouble with this. I think that after your first query you were possibly overwriting the value of `Table.column`, which is why I asked you to share your code. At any rate, let me know if you want some help.