[GH-ISSUE #9] error #7

Open
opened 2026-02-27 10:17:01 +03:00 by kerem · 1 comment
Owner

Originally created by @MuskanNazim on GitHub (Sep 1, 2024).
Original GitHub issue: https://github.com/mendsalbert/ai-finance-tracking/issues/9

Unhandled Runtime Error
NeonDbError: function sum(character varying) does not exist

Source
app(routes)\dashboard\page.jsx (25:20) @ async getBudgetList

23 | */
24 | const getBudgetList = async () => {

25 | const result = await db
| ^
26 | .select({
27 | ...getTableColumns(Budgets),
28 |

Unhandled Runtime Error
NeonDbError: function sum(character varying) does not exist

Source
app(routes)\dashboard\budgets_components\BudgetList.jsx (25:20) @ async getBudgetList

23 | */
24 | const getBudgetList = async () => {

25 | const result = await db.select({
| ^
26 | ...getTableColumns(Budgets),
27 | totalSpend: sql sum(${Expenses.amount}).mapWith(Number),
28 | totalItem: sql count(${Expenses.id}).mapWith(Number)

Originally created by @MuskanNazim on GitHub (Sep 1, 2024). Original GitHub issue: https://github.com/mendsalbert/ai-finance-tracking/issues/9 Unhandled Runtime Error NeonDbError: function sum(character varying) does not exist Source app\(routes)\dashboard\page.jsx (25:20) @ async getBudgetList 23 | */ 24 | const getBudgetList = async () => { > 25 | const result = await db | ^ 26 | .select({ 27 | ...getTableColumns(Budgets), 28 | Unhandled Runtime Error NeonDbError: function sum(character varying) does not exist Source app\(routes)\dashboard\budgets\_components\BudgetList.jsx (25:20) @ async getBudgetList 23 | */ 24 | const getBudgetList = async () => { > 25 | const result = await db.select({ | ^ 26 | ...getTableColumns(Budgets), 27 | totalSpend: sql `sum(${Expenses.amount})`.mapWith(Number), 28 | totalItem: sql `count(${Expenses.id})`.mapWith(Number)
Author
Owner

@codeautopilot[bot] commented on GitHub (Sep 1, 2024):

Potential solution

The plan to solve the bug involves ensuring that the amount field in the Expenses, Budgets, and Incomes tables is of a numeric type. This will allow the sum function to operate correctly without causing a runtime error. Additionally, we will update the getBudgetList function to cast the amount field to a numeric type if necessary.

What is causing this bug?

The bug is caused by the amount field in the Budgets and Incomes tables being defined as character varying (string) instead of a numeric type. The sum function in SQL requires a numeric data type, and when it encounters a string, it throws an error. This issue is evident in the getBudgetList function, where the sum function is applied to the amount field.

Code

Schema Update

First, we need to update the schema definitions in utils/schema.jsx to ensure that the amount field is of a numeric type in all relevant tables.

import {
  integer,
  numeric,
  pgTable,
  serial,
  varchar,
} from "drizzle-orm/pg-core";

export const Budgets = pgTable("budgets", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull().default(0), // Updated to numeric
  icon: varchar("icon"),
  createdBy: varchar("createdBy").notNull(),
});

export const Incomes = pgTable("incomes", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull().default(0), // Updated to numeric
  icon: varchar("icon"),
  createdBy: varchar("createdBy").notNull(),
});

export const Expenses = pgTable("expenses", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull().default(0),
  budgetId: integer("budgetId").references(() => Budgets.id),
  createdAt: varchar("createdAt").notNull(),
});

Function Update

Next, we need to update the getBudgetList function in both app/(routes)/dashboard/page.jsx and app/(routes)/dashboard/budgets/_components/BudgetList.jsx to ensure that the amount field is cast to a numeric type before applying the sum function.

app/(routes)/dashboard/page.jsx

const getBudgetList = async () => {
  const result = await db
    .select({
      ...getTableColumns(Budgets),
      totalSpend: sql`SUM(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number),
      totalItem: sql`count(${Expenses.id})`.mapWith(Number),
    })
    .from(Budgets)
    .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
    .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress))
    .groupBy(Budgets.id)
    .orderBy(desc(Budgets.id));
  setBudgetList(result);
  getAllExpenses();
  getIncomeList();
};

app/(routes)/dashboard/budgets/_components/BudgetList.jsx

const getBudgetList = async () => {
  const result = await db.select({
    ...getTableColumns(Budgets),
    totalSpend: sql `SUM(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number),
    totalItem: sql `count(${Expenses.id})`.mapWith(Number)
  }).from(Budgets)
    .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
    .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress))
    .groupBy(Budgets.id)
    .orderBy(desc(Budgets.id));

  setBudgetList(result);
}

How to replicate the bug

  1. Ensure the amount field in the Budgets or Incomes table is defined as varchar.
  2. Attempt to run the getBudgetList function in either app/(routes)/dashboard/page.jsx or app/(routes)/dashboard/budgets/_components/BudgetList.jsx.
  3. Observe the runtime error indicating that the sum function does not exist for character varying.

By following these steps, you should encounter the same runtime error, confirming the presence of the bug. After applying the proposed solution, the error should be resolved.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on app/(routes)/dashboard/page.jsx

Bug Analysis Report

File: app/(routes)/dashboard/page.jsx

Issue Description

The user reported an unhandled runtime error:

Unhandled Runtime Error
NeonDbError: function sum(character varying) does not exist

This error occurs in the getBudgetList function when attempting to sum the amount field from the Expenses table.

Analysis

The error message indicates that the sum function is being called on a field of type character varying (string), which is not supported. The sum function in SQL requires a numeric data type.

Relevant Code Snippet

const getBudgetList = async () => {
  const result = await db
    .select({
      ...getTableColumns(Budgets),
      totalSpend: sql`sum(${Expenses.amount})`.mapWith(Number),
      totalItem: sql`count(${Expenses.id})`.mapWith(Number),
    })
    .from(Budgets)
    .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
    .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress))
    .groupBy(Budgets.id)
    .orderBy(desc(Budgets.id));
  setBudgetList(result);
  getAllExpenses();
  getIncomeList();
};

Potential Cause

The amount field in the Expenses table is likely defined as a character varying (string) type instead of a numeric type. This causes the sum function to fail.

Suggested Fix

To resolve this issue, the amount field in the Expenses table should be cast to a numeric type before applying the sum function. This can be done using the CAST function in SQL.

Updated Code

Update the getBudgetList function to cast the amount field to a numeric type:

const getBudgetList = async () => {
  const result = await db
    .select({
      ...getTableColumns(Budgets),
      totalSpend: sql`SUM(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number),
      totalItem: sql`count(${Expenses.id})`.mapWith(Number),
    })
    .from(Budgets)
    .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
    .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress))
    .groupBy(Budgets.id)
    .orderBy(desc(Budgets.id));
  setBudgetList(result);
  getAllExpenses();
  getIncomeList();
};

Conclusion

The root cause of the issue is the incorrect data type of the amount field in the Expenses table. By casting the amount field to a numeric type, the sum function will work correctly, resolving the runtime error.

This file is relevant to the bug, and the suggested fix should be implemented to handle the correct data type for the amount field.

Changes on utils/schema.jsx

Bug Analysis Report

File: utils/schema.jsx

Summary

The file defines the schema for three tables: Budgets, Incomes, and Expenses using the drizzle-orm library. The issue reported is related to a runtime error indicating that the sum function does not exist for the character varying type, which suggests a type mismatch in the database schema.

Analysis

The error message indicates that the sum function is being called on a character varying type, which is not supported. This suggests that the amount field in one of the tables is defined as a varchar instead of a numeric type.

Findings

  • The amount field in the Budgets table is defined as varchar("amount").notNull().
  • The amount field in the Incomes table is also defined as varchar("amount").notNull().
  • The amount field in the Expenses table is correctly defined as numeric("amount").notNull().default(0).

Cause of the Bug

The amount field in the Budgets table is defined as a varchar, which is causing the sum function to fail. The sum function expects a numeric type, but it is receiving a character varying type instead.

Recommendation

Update the amount field in the Budgets table to be of a numeric type. This will ensure that the sum function can operate correctly on this field.

Updated Code

import {
  integer,
  numeric,
  pgTable,
  serial,
  varchar,
} from "drizzle-orm/pg-core";

export const Budgets = pgTable("budgets", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull().default(0), // Updated to numeric
  icon: varchar("icon"),
  createdBy: varchar("createdBy").notNull(),
});

export const Incomes = pgTable("incomes", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull().default(0), // Updated to numeric
  icon: varchar("icon"),
  createdBy: varchar("createdBy").notNull(),
});

export const Expenses = pgTable("expenses", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull().default(0),
  budgetId: integer("budgetId").references(() => Budgets.id),
  createdAt: varchar("createdAt").notNull(),
});

Conclusion

The primary cause of the bug is the incorrect data type for the amount field in the Budgets and Incomes tables. Changing the data type to numeric will resolve the issue.

Changes on app/(routes)/dashboard/budgets/_components/BudgetList.jsx

Bug Analysis Report

File: app/(routes)/dashboard/budgets/_components/BudgetList.jsx

Issue Description

The user reported an unhandled runtime error:

Unhandled Runtime Error
NeonDbError: function sum(character varying) does not exist

This error occurs in the getBudgetList function when trying to execute the following SQL query:

totalSpend: sql `sum(${Expenses.amount})`.mapWith(Number),

Analysis

The error message indicates that the sum function is being called on a character varying (string) type, which is not supported. The sum function in SQL expects a numeric type.

Root Cause

The root cause of the error is that the amount field in the Expenses table is of type character varying (string) instead of a numeric type. This causes the sum function to fail.

Proposed Solution

  1. Ensure the amount field in the Expenses table is of a numeric type:

    • This should be verified and updated in the utils/schema.jsx file.
  2. Update the getBudgetList function to handle the correct data type for the amount field:

    • Ensure that the amount field is correctly cast to a numeric type if necessary.

Updated Code

Here is the updated getBudgetList function to ensure it handles the correct data type for the amount field:

const getBudgetList = async () => {
  const result = await db.select({
    ...getTableColumns(Budgets),
    totalSpend: sql `sum(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number),
    totalItem: sql `count(${Expenses.id})`.mapWith(Number)
  }).from(Budgets)
    .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId))
    .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress))
    .groupBy(Budgets.id)
    .orderBy(desc(Budgets.id));

  setBudgetList(result);
}

This code casts the amount field to a numeric type before applying the sum function.

Conclusion

The primary issue in this file is the incorrect data type of the amount field in the Expenses table. By ensuring the amount field is of a numeric type and updating the getBudgetList function to handle this correctly, the error should be resolved. Further verification and updates may be needed in the utils/schema.jsx file to ensure the amount field is defined as a numeric type.

Disclaimer: This comment was entirely generated using AI. Be aware that the information provided may be incorrect.

Current plan usage: 17.62%

Have feedback or need help?
Discord
Documentation
support@codeautopilot.com

<!-- gh-comment-id:2323180127 --> @codeautopilot[bot] commented on GitHub (Sep 1, 2024): # Potential solution The plan to solve the bug involves ensuring that the `amount` field in the `Expenses`, `Budgets`, and `Incomes` tables is of a numeric type. This will allow the `sum` function to operate correctly without causing a runtime error. Additionally, we will update the `getBudgetList` function to cast the `amount` field to a numeric type if necessary. # What is causing this bug? The bug is caused by the `amount` field in the `Budgets` and `Incomes` tables being defined as `character varying` (string) instead of a numeric type. The `sum` function in SQL requires a numeric data type, and when it encounters a string, it throws an error. This issue is evident in the `getBudgetList` function, where the `sum` function is applied to the `amount` field. # Code ## Schema Update First, we need to update the schema definitions in `utils/schema.jsx` to ensure that the `amount` field is of a numeric type in all relevant tables. ```jsx import { integer, numeric, pgTable, serial, varchar, } from "drizzle-orm/pg-core"; export const Budgets = pgTable("budgets", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull().default(0), // Updated to numeric icon: varchar("icon"), createdBy: varchar("createdBy").notNull(), }); export const Incomes = pgTable("incomes", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull().default(0), // Updated to numeric icon: varchar("icon"), createdBy: varchar("createdBy").notNull(), }); export const Expenses = pgTable("expenses", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull().default(0), budgetId: integer("budgetId").references(() => Budgets.id), createdAt: varchar("createdAt").notNull(), }); ``` ## Function Update Next, we need to update the `getBudgetList` function in both `app/(routes)/dashboard/page.jsx` and `app/(routes)/dashboard/budgets/_components/BudgetList.jsx` to ensure that the `amount` field is cast to a numeric type before applying the `sum` function. ### `app/(routes)/dashboard/page.jsx` ```javascript const getBudgetList = async () => { const result = await db .select({ ...getTableColumns(Budgets), totalSpend: sql`SUM(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number), totalItem: sql`count(${Expenses.id})`.mapWith(Number), }) .from(Budgets) .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId)) .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress)) .groupBy(Budgets.id) .orderBy(desc(Budgets.id)); setBudgetList(result); getAllExpenses(); getIncomeList(); }; ``` ### `app/(routes)/dashboard/budgets/_components/BudgetList.jsx` ```javascript const getBudgetList = async () => { const result = await db.select({ ...getTableColumns(Budgets), totalSpend: sql `SUM(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number), totalItem: sql `count(${Expenses.id})`.mapWith(Number) }).from(Budgets) .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId)) .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress)) .groupBy(Budgets.id) .orderBy(desc(Budgets.id)); setBudgetList(result); } ``` # How to replicate the bug 1. Ensure the `amount` field in the `Budgets` or `Incomes` table is defined as `varchar`. 2. Attempt to run the `getBudgetList` function in either `app/(routes)/dashboard/page.jsx` or `app/(routes)/dashboard/budgets/_components/BudgetList.jsx`. 3. Observe the runtime error indicating that the `sum` function does not exist for `character varying`. By following these steps, you should encounter the same runtime error, confirming the presence of the bug. After applying the proposed solution, the error should be resolved. [Click here to create a Pull Request with the proposed solution](https://api.codeautopilot.com/create-pr?task=30730&hash=12b3cab4140649f0ce678072ac5d089eb176ef3a562f8f6f8d4fb4e5b21a6fe7) **Files used for this task:** <details> <summary>Changes on app/(routes)/dashboard/page.jsx</summary> # Bug Analysis Report ## File: `app/(routes)/dashboard/page.jsx` ### Issue Description The user reported an unhandled runtime error: ``` Unhandled Runtime Error NeonDbError: function sum(character varying) does not exist ``` This error occurs in the `getBudgetList` function when attempting to sum the `amount` field from the `Expenses` table. ### Analysis The error message indicates that the `sum` function is being called on a field of type `character varying` (string), which is not supported. The `sum` function in SQL requires a numeric data type. ### Relevant Code Snippet ```javascript const getBudgetList = async () => { const result = await db .select({ ...getTableColumns(Budgets), totalSpend: sql`sum(${Expenses.amount})`.mapWith(Number), totalItem: sql`count(${Expenses.id})`.mapWith(Number), }) .from(Budgets) .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId)) .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress)) .groupBy(Budgets.id) .orderBy(desc(Budgets.id)); setBudgetList(result); getAllExpenses(); getIncomeList(); }; ``` ### Potential Cause The `amount` field in the `Expenses` table is likely defined as a `character varying` (string) type instead of a numeric type. This causes the `sum` function to fail. ### Suggested Fix To resolve this issue, the `amount` field in the `Expenses` table should be cast to a numeric type before applying the `sum` function. This can be done using the `CAST` function in SQL. ### Updated Code Update the `getBudgetList` function to cast the `amount` field to a numeric type: ```javascript const getBudgetList = async () => { const result = await db .select({ ...getTableColumns(Budgets), totalSpend: sql`SUM(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number), totalItem: sql`count(${Expenses.id})`.mapWith(Number), }) .from(Budgets) .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId)) .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress)) .groupBy(Budgets.id) .orderBy(desc(Budgets.id)); setBudgetList(result); getAllExpenses(); getIncomeList(); }; ``` ### Conclusion The root cause of the issue is the incorrect data type of the `amount` field in the `Expenses` table. By casting the `amount` field to a numeric type, the `sum` function will work correctly, resolving the runtime error. This file is relevant to the bug, and the suggested fix should be implemented to handle the correct data type for the `amount` field. </details> <details> <summary>Changes on utils/schema.jsx</summary> # Bug Analysis Report ## File: `utils/schema.jsx` ### Summary The file defines the schema for three tables: `Budgets`, `Incomes`, and `Expenses` using the `drizzle-orm` library. The issue reported is related to a runtime error indicating that the `sum` function does not exist for the `character varying` type, which suggests a type mismatch in the database schema. ### Analysis The error message indicates that the `sum` function is being called on a `character varying` type, which is not supported. This suggests that the `amount` field in one of the tables is defined as a `varchar` instead of a numeric type. ### Findings - The `amount` field in the `Budgets` table is defined as `varchar("amount").notNull()`. - The `amount` field in the `Incomes` table is also defined as `varchar("amount").notNull()`. - The `amount` field in the `Expenses` table is correctly defined as `numeric("amount").notNull().default(0)`. ### Cause of the Bug The `amount` field in the `Budgets` table is defined as a `varchar`, which is causing the `sum` function to fail. The `sum` function expects a numeric type, but it is receiving a `character varying` type instead. ### Recommendation Update the `amount` field in the `Budgets` table to be of a numeric type. This will ensure that the `sum` function can operate correctly on this field. ### Updated Code ```jsx import { integer, numeric, pgTable, serial, varchar, } from "drizzle-orm/pg-core"; export const Budgets = pgTable("budgets", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull().default(0), // Updated to numeric icon: varchar("icon"), createdBy: varchar("createdBy").notNull(), }); export const Incomes = pgTable("incomes", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull().default(0), // Updated to numeric icon: varchar("icon"), createdBy: varchar("createdBy").notNull(), }); export const Expenses = pgTable("expenses", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull().default(0), budgetId: integer("budgetId").references(() => Budgets.id), createdAt: varchar("createdAt").notNull(), }); ``` ### Conclusion The primary cause of the bug is the incorrect data type for the `amount` field in the `Budgets` and `Incomes` tables. Changing the data type to `numeric` will resolve the issue. </details> <details> <summary>Changes on app/(routes)/dashboard/budgets/_components/BudgetList.jsx</summary> # Bug Analysis Report ## File: `app/(routes)/dashboard/budgets/_components/BudgetList.jsx` ### Issue Description The user reported an unhandled runtime error: ``` Unhandled Runtime Error NeonDbError: function sum(character varying) does not exist ``` This error occurs in the `getBudgetList` function when trying to execute the following SQL query: ```javascript totalSpend: sql `sum(${Expenses.amount})`.mapWith(Number), ``` ### Analysis The error message indicates that the `sum` function is being called on a `character varying` (string) type, which is not supported. The `sum` function in SQL expects a numeric type. ### Root Cause The root cause of the error is that the `amount` field in the `Expenses` table is of type `character varying` (string) instead of a numeric type. This causes the `sum` function to fail. ### Proposed Solution 1. **Ensure the `amount` field in the `Expenses` table is of a numeric type**: - This should be verified and updated in the `utils/schema.jsx` file. 2. **Update the `getBudgetList` function to handle the correct data type for the `amount` field**: - Ensure that the `amount` field is correctly cast to a numeric type if necessary. ### Updated Code Here is the updated `getBudgetList` function to ensure it handles the correct data type for the `amount` field: ```javascript const getBudgetList = async () => { const result = await db.select({ ...getTableColumns(Budgets), totalSpend: sql `sum(CAST(${Expenses.amount} AS NUMERIC))`.mapWith(Number), totalItem: sql `count(${Expenses.id})`.mapWith(Number) }).from(Budgets) .leftJoin(Expenses, eq(Budgets.id, Expenses.budgetId)) .where(eq(Budgets.createdBy, user?.primaryEmailAddress?.emailAddress)) .groupBy(Budgets.id) .orderBy(desc(Budgets.id)); setBudgetList(result); } ``` This code casts the `amount` field to a numeric type before applying the `sum` function. ### Conclusion The primary issue in this file is the incorrect data type of the `amount` field in the `Expenses` table. By ensuring the `amount` field is of a numeric type and updating the `getBudgetList` function to handle this correctly, the error should be resolved. Further verification and updates may be needed in the `utils/schema.jsx` file to ensure the `amount` field is defined as a numeric type. </details> <footer> _Disclaimer: This comment was entirely generated using AI. Be aware that the information provided may be incorrect._ Current plan usage: 17.62% **Have feedback or need help?** [Discord](https://discord.gg/r72ykfvyx7) [Documentation](https://docs.codeautopilot.com/) [support@codeautopilot.com](mailto:support@codeautopilot.com) </footer>
Sign in to join this conversation.
No labels
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/ai-finance-tracking-mendsalbert#7
No description provided.