2

I want to use ORM from DataStax Python Driver for Cassandra DB (create some class with data and automatically create a table from it without writing too much CQL = )

I've deployed a Cassandra server on localhost via docker and tried to do just like it's written in their manual:

from cassandra.cluster import Cluster

from cassandra.cqlengine import columns
from cassandra.cqlengine.models import Model
from cassandra.cqlengine.management import sync_table, create_keyspace_simple


class Person(Model):
    __keyspace__ = 'mega_keyspace'
    __table_name__ = 'person'
    id = columns.UUID(primary_key=True)
    first_name = columns.Text()
    last_name = columns.Text()


if __name__ == "__main__":
    cluster = Cluster()
    session = cluster.connect()
    # create_keyspace_simple("mega_keyspace", 2)
    session.execute("CREATE KEYSPACE IF NOT EXISTS mega_keyspace WITH REPLICATION = "
                "{ 'class' : 'SimpleStrategy', 'replication_factor' : 2 };")  # keyspace is created okay...
    sync_table(Person)  # And here's the error appears!

But alas, sync_table(...) gives me an error:

cassandra.cqlengine.CQLEngineException: Connection name '<object object at 0x7fbd95322ab0>' doesn't exist in the registry.

How can I fix it?

Felix
  • 3,351
  • 6
  • 40
  • 68

1 Answers1

0

You can probably solve the issue by adding the two following lines to your code

from cassandra.cqlengine.connection import set_default_connection, register_coneection

add the following lines in the main function after the session definition

register_connection(str(session), session=session)
set_default_conection(session)
Esmaeil MIRZAEE
  • 1,110
  • 11
  • 14