r4fek / django-cassandra-engine

Django Cassandra Engine - the Cassandra backend for Django
BSD 2-Clause "Simplified" License
365 stars 84 forks source link

Despite cassandra_connection.connected being True getting CQLEngineException #49

Closed baali closed 8 years ago

baali commented 8 years ago

Hello,

I have django_cassandra_engine version 0.6.3 with 3.0 cassandra-driver and Django1.7 installed. With previous versions of django_cassandra_engine views involving Cassandra Models uses to work out of box but with the updates I am getting CQLEngineException: It is required to setup() cqlengine before executing queries From manage.py shell I have tried to do things manually but still same error:

In [1]: from django_cassandra_engine.utils import get_cassandra_connection In [2]: cassandra_connection = get_cassandra_connection() In [3]: cassandra_connection.connect() In [7]: SalaryRecords.objects.count() CQLEngineException Traceback (most recent call last)

in () ----> 1 SalaryRecords.objects.count() CQLEngineException: It is required to setup() cqlengine before executing queries In [8]: cassandra_connection.connected Out[8]: True

Doing setup directly and running the query works.

In [9]: from django.conf import settings In [11]: from cqlengine.connection import setup In [12]: setup(hosts=[settings.DATABASES['cassandra']['HOST']], default_keyspace=settings.DATABASES['cassandra']['NAME']) setup(hosts=[settings.DATABASES['cassandra']['HOST']], default_keyspace=settings.DATABASES['cassandra']['NAME']) In [13]: SalaryRecords.objects.count() Out[13]: 8952

What am I missing?

r4fek commented 8 years ago

Hi @baali,

thanks for your report. Are you sure you set up DATABASES dict in settings.py correctly? Could you paste it? If cassandra_connection.connected is set to True, then cqlengine.connection.setup is being called for sure.

r4fek commented 8 years ago

@baali please try this from shell:

from django.db import connection
connection.connect()

and let me know if it works. This will work if your only database is cassandra. If not:

from django.db import connections
connections['cassandra'].connect()
baali commented 8 years ago

Hello @r4fek Thank you for getting back. Yes doing connections['cassandra'].connect() Worked. But trying to run a count query is still throwing up same CQLEngineException. My DATABASES dict in settings.py is this:

'cassandra': {'ATOMIC_REQUESTS': False, 'AUTOCOMMIT': True, 'CONN_MAX_AGE': 0, 'ENGINE': 'django_cassandra_engine', 'HOST': 'localhost', 'NAME': 'playground', 'OPTIONS': {'connection': {'auth_provider': <cassandra.auth.PlainTextAuthProvider at 0x7f7e0c2a7690>, 'consistency': 10, 'lazy_connect': True, 'protocol_version': 3, 'retry_connect': True}, 'replication': {'replication_factor': 1, 'strategy_class': 'SimpleStrategy'}, 'session': {'default_fetch_size': 10000, 'default_timeout': 20}}, 'PASSWORD': 'pass', 'PORT': '', 'TEST': {'CHARSET': None, 'COLLATION': None, 'MIRROR': None, 'NAME': 'test_db'}, 'TIME_ZONE': 'UTC', 'USER': 'user'},

r4fek commented 8 years ago

@baali I checked the testproject from this repo with cassandra as secondary backend:

$ cd django-cassandra-engine/testproject
$ export DJANGO_SETTINGS_MODULE="settings.secondary_cassandra"
$ python manage.py sync_cassandra
Creating keyspace db..
Syncing app.models.ExampleModel
Syncing app.models.ExampleModel2
Syncing app.models.TestProjectModel
$ python manage.py shell
Python 2.7.8 (default, Oct 20 2014, 15:05:19) 
[GCC 4.9.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from app.models import *
>>> ExampleModel.objects.create(id=1)
ExampleModel(id=1, created_at=None, deleted=False)
>>> list(ExampleModel.objects.all())
[ExampleModel(id=1, created_at=None, deleted=False)]

As you can see it worked out of the box.

My pip freeze is:

Django==1.7.7
Jinja2==2.8
Markdown==2.6.2
MarkupSafe==0.23
PyYAML==3.11
argparse==1.2.1
backports.ssl-match-hostname==3.4.0.2
blist==1.3.6
cassandra-driver==3.0.0
certifi==2015.04.28
click==5.1
cqlengine==0.21.0
django-cassandra-engine==0.6.0
djangotoolbox==1.6.2
futures==3.0.3
livereload==2.4.0
mkdocs==0.14.0
mock==1.0.1
six==1.10.0
tornado==4.2.1
wsgiref==0.1.2

Could you confirm that this works for you too?

baali commented 8 years ago

Hello @r4fek Yes this works for me too.

[baali@jugaad] [~/analytics/django-cassandra-engine/testproject]
 $ export DJANGO_SETTINGS_MODULE="settings.secondary_cassandra"
(tsenv)[baali@jugaad] [~/analytics/django-cassandra-engine/testproject]
 $ python manage.py sync_cassandra
Creating keyspace db..
Syncing app.models.ExampleModel
Syncing app.models.ExampleModel2
Syncing app.models.TestProjectModel
(tsenv)[baali@jugaad] [~/analytics/django-cassandra-engine/testproject]
 $ python manage.py shell
Python 2.7.6 (default, Mar 22 2014, 22:59:56) 
Type "copyright", "credits" or "license" for more information.

IPython 0.13.2 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from app.models import *

In [2]: ExampleModel.objects.create(id=1)
Out[2]: ExampleModel(id=1, created_at=None, deleted=False)

In [3]: list(ExampleModel.objects.all())
Out[3]: [ExampleModel(id=1, created_at=None, deleted=False)]

My pip freeze is:

 $ pip freeze|grep cass
cassandra-driver==3.0.0
django-cassandra-engine==0.6.0
$ pip freeze | grep Django
Django==1.7.7
$ pip freeze | grep cql
cqlengine==0.21.0

I am not sure, seems like there is some issue with my configs/local setup. Sorry for the trouble I will try to cross check and update the thread.

r4fek commented 8 years ago

Hi, @baali. Any updates on this? Did you manage to fix it?

baali commented 8 years ago

Hey @r4fek. No, I started making direct queries. Sorry for the trouble, closing this.