ModelDefinitionException("At least 1 primary key is required.") even when Primary key is already defined

320 views Asked by At

I am using Cqlalchemy to interact with Cassandra db using Python. While running the application, I am facing error

raise ModelDefinitionException("At least 1 primary key is required.")
cassandra.cqlengine.models.ModelDefinitionException: At least 1 primary key is required.

Models looks like:

class Model1(db.Model):
    id = db.columns.UUID(primary_key=True, default=uuid.uuid4),
    type = db.columns.Text(required=False)
    user_id = db.columns.UUID(primary_key=True, default=uuid.uuid4)

Where db = CQLAlchemy()

Cassandra table DDL is:

CREATE TABLE keyspace1.model1 (
    id uuid,
    user_id uuid,
    type text,
    PRIMARY KEY (id, user_id)
)

EDIT: I went to debug it at cqlengine/models.py file level. What I noticed was all the columns were being captured as instance of tuple

 'id': (<cqlengine.columns.UUID object at 0x7f86b2c7c940>,), 'user_id': (<cqlengine.columns.UUID object at 0x7f86b2c7c9b0>,)

And cqlengine filters out keys from attrs key by checking if its a type of columns.Column.

column_definitions = [(k, v) for k, v in attrs.items() if isinstance(v, columns.Column)]

Github link for cqlengine/models.py

That's why its actually not able to get any column and hence says no primary key defined.

Any idea why columns are being picked as tuple type and not columns.Column?

Thanks

0

There are 0 answers