pythoncassandracqlengine

cqlengine connection set_session() not handling different keyspaces


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?


Solution

  • 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.