sdispater/orator-cache

Caching not working with Orator ORM

joaobarbosa opened this issue · 6 comments

I'm having some problems when trying to use the caching with the ORM.

Setup:

  • Orator@0.8 (straight from github)
  • Python 3.5
  • orator-cache 0.1.1

Here is a sample of whats happening:

from orator import Model
from orator_cache import DatabaseManager, Cache
from user_model import User

DATABASE = {
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost',
        'database': 'existing_db',
        'user': 'valid_user',
        'password': 'valid_pass',
    }
}

DB_CACHE = {
    'stores': {'file': {'driver': 'file', 'path': 'project/cache'}}
}

db = DatabaseManager(DATABASE, cache=Cache(DB_CACHE))
Model.set_connection_resolver(db)

# Works!
users = db.table('users').remember(10).get()

# Does not Work
users = User.get()
# raises TypeError: __init__() missing 1 required positional argument: 'cache'
# User.all() raises the same exception
# User.remember(10).get() raises the same exception

And a traceback:

Traceback (most recent call last):
  File "/home/user/python-projects/scout/scout_app/blueprints/scout.py", line 37, in home
    teams = Team.all()
  File "/home/user/.virtualenvs/scout/lib/python3.5/site-packages/orator/orm/model.py", line 545, in all
    return instance.new_query().get(columns)
  File "/home/user/.virtualenvs/scout/lib/python3.5/site-packages/orator/orm/builder.py", line 198, in get
    models = self.get_models(columns)
  File "/home/user/.virtualenvs/scout/lib/python3.5/site-packages/orator/orm/builder.py", line 439, in get_models
    results = self.apply_scopes().get_query().get(columns)
  File "/home/user/.virtualenvs/scout/lib/python3.5/site-packages/orator/orm/builder.py", line 936, in apply_scopes
    builder = copy.copy(self)
  File "/home/user/.virtualenvs/scout/lib/python3.5/copy.py", line 89, in copy
    return copier(x)
  File "/home/user/.virtualenvs/scout/lib/python3.5/site-packages/orator/orm/builder.py", line 1171, in __copy__
    new = self.__class__(copy.copy(self._query))
  File "/home/user/.virtualenvs/scout/lib/python3.5/copy.py", line 89, in copy
    return copier(x)
  File "/home/user/.virtualenvs/scout/lib/python3.5/site-packages/orator/query/builder.py", line 1671, in __copy__
    new = self.__class__(self._connection, self._grammar, self._processor)
TypeError: __init__() missing 1 required positional argument: 'cache'

Thanks!

@sdispater any thoughts on that?

I think it's linked to changes made in the 0.8 version of Orator when cloning the builder class.

orator-cache uses a custom query builder which expects a Cache object that is not being copied.

I will fix it and get back to you.

It seems that the error only occurs when a global scope is applied on the model (for instance the builtin SoftDeletes).

Can you confirm that this is the case?

As soon as I can I will test it out.

I just pushed a commit (f2510c7) on the develop branch that should fix the problem.

As far as I tested it out, it's working perfectly!

Thanks again! Congrats for the job you've been doing with Orator ORM and related libs. :)