1

I've seen questions very similar to this, but none of the answers are ideal because they're either inefficient or don't work.

I have a database table which has the following rows:

id feed datetime content

I need to iterate over every item in the table where feed is example (or any other value; always a string) but the iteration needs to be ordered by datetime.

Unfortunately I can't do it in memory because there could be hundreds of thousands of entries for a single user.

I've tried modifying code found here and didn't make any progress. I've also adapted the best answer here and get an error when I run this;

print r.db('main').table('data').between([feedid, r.minval], [feedid, r.maxval], index="feedByTime").order_by(index="feedByTime").run(conn)

Which throws this error;

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/rethinkdb/net.py", line 196, in __str__ val_str = ', '.join(map(repr, self.items[:10])) TypeError: sequence index must be integer, not 'slice'

If I remove index="feedByTime" from between, I get this error instead;

rethinkdb.errors.ReqlQueryLogicError: Cannot order by index 'feedByTime' after calling BETWEEN on index 'id'

The index (feedByTime) was made like so;

r.db('main').table('data').index_create("feedByTime", lambda x: [x["feed"], x["datetime"]]).run(conn)

Any suggestions would be great, thanks in advance.

Community
  • 1
  • 1
Joseph
  • 193
  • 11
  • That code looks correct to me. Are you running the latest version of the Python driver? – mlucy Mar 26 '16 at 19:43
  • @mlucy Yeah, everything is on the latest version. Nothing I've tried is working, I must be missing something. – Joseph Mar 26 '16 at 20:40
  • That error looks like it might be a bug in the Python driver. I would open a bug on the GitHub issue tracker: https://github.com/rethinkdb/rethinkdb/issues – mlucy Mar 27 '16 at 06:05
  • Yes, I believe that might be the case. I edited the Python Driver to add an exception which 'fixed' it. I'll open a bug report. Seems like it just needs to handle that exception in a more friendly way rather than halting the whole query from being run. I hadn't actually considered a Driver bug before your comment, so thank you. :) – Joseph Mar 28 '16 at 12:48

1 Answers1

0

Your query has an issue:

Instead of

query = (
    r.db('main').table('data')
     .between(
         [feedid, r.minval],
         [feedid, r.maxval],
         index="feedByTime"
     )
     .order_by(index="feedByTime")
     .run(conn)
)

Try

query = (
    r.db('main').table('data')
     .between(
         [feedid, r.minval],
         [feedid, r.maxval],
         index="feedByTime"
     )
     .order('datetime')
     .run(conn)
)
DevLounge
  • 7,345
  • 2
  • 26
  • 39