Usman Rafiq
Usman Rafiq

Reputation: 590

Cassandra: "Model keyspace not set" and "Connection name doesn't exist in the registry" Errors

I recently upgraded to Django 4.2 and am facing issues with Cassandra integration. Initially, I encountered the following error when attempting to generate queries:

cassandra.cqlengine.CQLEngineException: Model keyspace is not set and no default is available. Set model keyspace or setup connection before attempting to generate a query.

This error was thrown despite specifying the model keyspace in settings.py. As an attempted fix, I explicitly set the keyspace in my models like so:

__keyspace__ = "my_keyspace"

However, this led to a new set of errors related to connection names not existing in the registry:

Connection name '<object object at 0x7f4fb00d67a0>' doesn't exist in the registry.

This issue persists across multiple models and queries. Here are some relevant details about my setup:

Here's a full error.

sessions, disconnected = fetch_sessions_list(ux_vms)
  File "/code/wtools2/apps/analytics/views.py", line 1652, in fetch_sessions_list
    metrics_session.extend(list(sessions_db))
  File "/usr/local/lib/python3.10/site-packages/cassandra/cqlengine/query.py", line 437, in __len__
    self._execute_query()
  File "/usr/local/lib/python3.10/site-packages/cassandra/cqlengine/query.py", line 472, in _execute_query
    self._result_generator = (i for i in self._execute(self._select_query()))
  File "/usr/local/lib/python3.10/site-packages/cassandra/cqlengine/query.py", line 456, in _select_query
    self.column_family_name,
  File "/usr/local/lib/python3.10/site-packages/cassandra/cqlengine/query.py", line 397, in column_family_name
    return self.model.column_family_name()
  File "/usr/local/lib/python3.10/site-packages/cassandra/cqlengine/models.py", line 559, in column_family_name
    raise CQLEngineException("Model keyspace is not set and no default is available. Set model keyspace or setup connection before attempting to generate a query.")

cassandra.cqlengine.CQLEngineException: Model keyspace is not set and no default is available. Set model keyspace or setup connection before attempting to generate a query.
raise CQLEngineException("Model keyspace is not set and no default is available. Set model keyspace or setup connection before attempting to generate a query.")
cassandra.cqlengine.CQLEngineException: Model keyspace is not set and no default is available. Set model keyspace or setup connection before attempting to generate a query.

Has anyone faced similar issues or have insights on resolving these errors in a Django 4.2 environment with Cassandra? Any help or guidance would be greatly appreciated.

Upvotes: 1

Views: 180

Answers (1)

Jared
Jared

Reputation: 11

This is how I was able to solve this same problem by creating a registered connection using a session. Note that I am using cassandra-driver and not django-cassandra-engine. I believe you would only need to substitute DjangoCassandraModel for Model for your use case.

from cassandra.cqlengine.connection import register_connection
_connection = register_connection(name=str(_session), session=_session, default=True)
    
from cassandra.cqlengine.models import Model
class MyModel(Model):
    ...
    # You can set connection & keyspace at class creation
    __connection__ = _connection.name
    __keyspace__ = _connection.session.keyspace

# You can set or change connection & keyspace after class creation
MyModel.__connection__ = _connection.name
MyModel.__keyspace__ = _connection.session.keyspace

Upvotes: 1

Related Questions