Forums

DISTINCT ON fields is not supported by this database backend

Hi All,

I want to get distinct objects from a filter below:

qt = FuelCardLog.objects.filter(requestor_uname=self.request.user).distinct('vehicle_no')

class FuelCardLog(models.Model):
    card_no = models.ForeignKey(FuelCard, related_name='%(class)s_card', on_delete=models.CASCADE,)
    vehicle_no = models.ForeignKey(Vehicle, related_name='%(class)s_vehicle', on_delete=models.CASCADE)
    requestor_uname = models.ForeignKey(User, related_name='%(class)s_requestor', on_delete=models.CASCADE)

class Vehicle(models.Model):
    vehicle_no = models.CharField(max_length=16, unique=True)

But I got this error.

"DISTINCT ON fields is not supported by this database backend"

Is it true that distinct is not supported on your version of mysql ? When I use database console to run mysql command. It works though.

SELECT DISTINCT `API_fuelcardlog`.`vehicle_no_id` FROM `API_fuelcardlog` WHERE (`API_fuelcardlog`.`requestor_uname_id` = 2 );

Any help is appreciated. Thanks.

distinct() called with fields works only with PostgreSQL as mentioned in the Django docs https://docs.djangoproject.com/en/3.1/ref/models/querysets/#distinct.