imranjust14
imranjust14

Reputation: 127

The partition key must be defined on delete queries in django-cassandra-engine

from django.utils import timezone
from cassandra.cqlengine import columns
from django_cassandra_engine.models import DjangoCassandraModel


class RequestResponse(DjangoCassandraModel):
    id = columns.UUID(primary_key=True, default=uuid.uuid4)
    sel_no = columns.Text(index=True)
    transaction_type = columns.Text()
    created_at = columns.DateTime(default=timezone.now())
    updated_at = columns.DateTime(default=timezone.now())

I am using django-cassandra-engine==1.6.1. When I am trying to delete my all data from this model then these errors occurred. My command line:

RequestResponse.objects.all().count()
>> 10123
RequestResponse.objects.all().delete()

Then these errors occurred.

    996         partition_keys = set(x.db_field_name for x in self.model._partition_keys.values())
    997         if partition_keys - set(c.field for c in self._where):
--> 998             raise QueryException("The partition key must be defined on delete queries")
    999 
   1000         dq = DeleteStatement(

QueryException: The partition key must be defined on delete queries

Upvotes: 0

Views: 320

Answers (1)

Erick Ramirez
Erick Ramirez

Reputation: 16353

Executing the equivalent of "delete all" will cause a full table scan and is not efficient in Cassandra so you can only delete a specific partition by providing the partition key. The underlying CQL statement is equivalent to:

DELETE FROM table_name WHERE pk = ?

But when you're trying to do a "delete all", it isn't valid in Cassandra since the DELETE must be restricted to a partition using the WHERE clause so it isn't valid in Django-Cassandra either.

If you don't need or want the data in the table, you should instead connect to your Cassandra instance directly with cqlsh and issue a TRUNCATE. Cheers!

Upvotes: 3

Related Questions