adewes / blitzdb

Blitz is a document-oriented database for Python that is backend-agnostic. It comes with a flat-file database for JSON documents and provides MongoDB-like querying capabilities.
http://blitzdb.readthedocs.org
MIT License
330 stars 37 forks source link

SqlBackend doesn't support querying without indexes, but it also can't create indexes #78

Open somelinguist opened 6 years ago

somelinguist commented 6 years ago

I'm using the SqlBackend with sqlite. I was able to populate the database, but when I tried to run a query, I got the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "...\lib\site-packages\blitzdb\backends\sql\backend.py", line 1408, in filter
    compiled_query = compile_query(collection,query)
  File "...\lib\site-packages\blitzdb\backends\sql\backend.py", line 1405, in compile_query
    raise AttributeError("Query over non-indexed field %s in collection %s!" % (key,collection))
AttributeError: Query over non-indexed field analyses in collection word!

I then tried to create an index but go the following error:

Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "...\lib\site-packages\blitzdb\backends\sql\backend.py", line 1071, in create_index
    self.db[collection].ensure_index(*args, **kwargs)
AttributeError: 'Backend' object has no attribute 'db'
adewes commented 6 years ago

hi @somelinguist ! The documentation is outdated right now, for the SQL backend you don't need to create indexes manually but you can specify them directly in your data models and create the database using backend.create_schema(). This will use SQLAlchemy to create your tables and initialize all indexes that you've defined.

For an example on how to write data models look here:

https://github.com/quantifiedcode/quantifiedcode/blob/master/quantifiedcode/backend/models

...or here:

https://github.com/quantifiedcode/checkmate/blob/master/checkmate/lib/models.py

You can also use Alembic to migrate your database database schemas (which is very handy when you do changes), for an example also look here:

https://github.com/quantifiedcode/quantifiedcode/tree/master/quantifiedcode/migrations