1

I have the following code

from cassandra.cqlengine import connection


#inside a flask function
session = get_session_for_keyspace(keyspace)
connection.set_session(session)

object_list = ModelTable.objects.filter(....)

But although session "points" to correct keyspace the select performed by filter in the ModelTable uses the old keypsace that was defined some lines before. How can I make Models use the session I set each time for the connection?

Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • Possible duplicate of [cqlengine multilpe keyspaces](http://stackoverflow.com/questions/30335649/cqlengine-multilpe-keyspaces) – TheGeorgeous Feb 17 '16 at 03:19

1 Answers1

2

Apostolos, it is using your session, however the default keyspace is set the FIRST time the set_session method is called. Look at this code here here in the cqlengine, connection class. I'm guessing your model doesn't have a keyspace defined and is therefore using the default one.

https://github.com/datastax/python-driver/blob/master/cassandra/cqlengine/connection.py#L85-88

If you are using a different model in different key spaces and are simply trying to toggle the connection class. You can define a default keyspace in your model, that should also work in this specific case.

Your model would need to override this value at the class level. https://github.com/datastax/python-driver/blob/master/cassandra/cqlengine/models.py#L311

Using the same model across multiple cassandra keyspaces is kind of fighting the way cqlengine is currently implemented. I would recommend you look at dropping down to the base driver and using that if possible in that case.

Greg Bestland
  • 713
  • 5
  • 6
  • But isn't each query executed using the execute method from connection class; and isn't execute using the session; and each session is connected using its own keys pace... Or have I misunderstood that; – Apostolos Feb 16 '16 at 17:06
  • 1
    I realize it's a bit confusing, but that's the nature of object mappers in general. What you describe is the way the base driver behaves. CQLENGINE on the otherhand obfuscates away the underlying queries. It constructs those queries using values provided by the Models. The __keyspace__ field in the model is used when determine the column family for various queries. You can see it being utilized here, https://github.com/datastax/python-driver/blob/master/cassandra/cqlengine/query.py#L329 That property is pulled directly from the model. Please take some time to look through the code. – Greg Bestland Feb 16 '16 at 18:04
  • These "details" should have been in the documentation! – Apostolos Feb 17 '16 at 08:22
  • This could be easier if it took the keyspace from the session in the connection class if the object's keyspace wasn't set, thus having more flexible objects. – Apostolos Feb 17 '16 at 08:49
  • I've opened an jira for discussion, https://datastax-oss.atlassian.net/browse/PYTHON-486 – Greg Bestland Feb 17 '16 at 18:30