SAMUEL Tuyizere
SAMUEL Tuyizere

Reputation: 79

How do i fix No function matches the given name and argument types. You might need to add explicit type casts

already_transferred = Transfer.objects.all().filter(transferred_by=request.user, account_id=id).aggregate(total=Coalesce(Sum('amount'), 0))

and the server is giving this error

function sum(text) does not exist
LINE 1: SELECT COALESCE(SUM("API_insidetransfer"."amount"), 0) AS "a...
                        ^
HINT:  No function matches the given name and argument types. You might need to add explicit type casts.

here is my model

class Transfer(models.Model):
    transferred_by = models.CharField(max_length=120)
    account_id = models.CharField(max_length=120)
    amount = models.CharField(max_length=120, default=0)
    created_on = models.DateTimeField(auto_now=True)

how can i fix this?

Upvotes: 2

Views: 2423

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476699

The problem is that your amount is a CharField. You can not sum charfields. What would be the sum of 'foo' and 'bar' for example.

You thus should fix the problem on the model level: make amount a DecimalField (or an IntegerField). For example:

class Transfer(models.Model):
    transferred_by = models.CharField(max_length=120)
    account_id = models.CharField(max_length=120)
    amount = modelsDecimalField(max_digits=12, decimal_places=2, default=0)
    created_on = models.DateTimeField(auto_now=True)

Upvotes: 3

Related Questions