Prisma-client-js: aggregateApi and sum returns null if no record

Created on 21 Aug 2020  路  2Comments  路  Source: prisma/prisma-client-js

Bug description

I'm using aggregateApi to sum an amount but if there is no record, the sum amount is null even though the type says number

How to reproduce

  1. Create an empty database with an Int field called amount
  2. Run const result = await prisma.disbursement.aggregate({ sum: { amount: true } })
  3. The return type of result.sum.amount is number. However, the value is null because there is no record
  4. Add one or more records and result.sum.amount returns the sum amount correctly

Expected behavior

The return type should either return number | null and/or give us an option to coalesce null to 0

Prisma information

model Disbursement {
  id               Int                @id @default(autoincrement())
  amount           Int
}

Environment & setup

  • OS: MacOS, running Docker with node:12.18.0-alpine3.11
  • Database: MySQL
  • Node.js version: 12.18.0
  • Prisma version: 2.5.1
@prisma/cli          : 2.5.1
Current platform     : linux-musl
Query Engine         : query-engine c88925ce44a9b89b4351aec85ba6a28979d2658e (at node_modules/@prisma/cli/query-engine-linux-musl)
Migration Engine     : migration-engine-cli c88925ce44a9b89b4351aec85ba6a28979d2658e (at node_modules/@prisma/cli/migration-engine-linux-musl)
Introspection Engine : introspection-core c88925ce44a9b89b4351aec85ba6a28979d2658e (at node_modules/@prisma/cli/introspection-engine-linux-musl)
Format Binary        : prisma-fmt c88925ce44a9b89b4351aec85ba6a28979d2658e (at node_modules/@prisma/cli/prisma-fmt-linux-musl)
Studio               : 0.261.0
Preview Features     : transactionApi
bu2-confirmed enginequery engine kinbug tecengines tectypescript aggregateApi

Most helpful comment

I can confirm this bug in dev build 2.6.0-dev.43. Thanks for reporting!

image

All 2 comments

I can confirm this bug in dev build 2.6.0-dev.43. Thanks for reporting!

image

Will be in the 2.7 release. Dev build is 2.7.0-dev.14 onwards.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

esistgut picture esistgut  路  4Comments

divyenduz picture divyenduz  路  4Comments

samrith-s picture samrith-s  路  3Comments

MichalLytek picture MichalLytek  路  3Comments

Errorname picture Errorname  路  3Comments