[GH-ISSUE #7] error #4

Open
opened 2026-02-27 10:17:00 +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/7

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 |

Originally created by @MuskanNazim on GitHub (Sep 1, 2024). Original GitHub issue: https://github.com/mendsalbert/ai-finance-tracking/issues/7 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 |
Author
Owner

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

Potential solution

The plan to solve the bug involves ensuring that the amount columns in both the Budgets and Expenses tables are of a numeric type. This will allow the sum function to operate correctly without causing a runtime error. The reasoning behind this solution is based on the error message indicating that the sum function does not exist for character varying types, which suggests that the amount columns are currently defined as varchar.

What is causing this bug?

The bug is caused by the amount column in the Budgets table being defined as varchar instead of a numeric type. This causes the sum function to fail because it expects a numeric type. Additionally, there might be a similar issue with the amount column in the Expenses table, although it is already defined as numeric. Ensuring both columns are numeric will resolve the issue.

Code

Step 1: Modify the Budgets table schema

Change the amount column in the Budgets table from varchar to numeric.

// File: utils/schema.jsx
export const Budgets = pgTable("budgets", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull(),  // Changed from varchar to numeric
  icon: varchar("icon"),
  createdBy: varchar("createdBy").notNull(),
});

Step 2: Ensure the Expenses table schema is correct

Verify that the amount column in the Expenses table is already defined as numeric.

// File: utils/schema.jsx
export const Expenses = pgTable("expenses", {
  id: serial("id").primaryKey(),
  name: varchar("name").notNull(),
  amount: numeric("amount").notNull(),  // Ensure this is numeric
  budgetId: integer("budgetId").references(() => Budgets.id),
  createdAt: varchar("createdAt").notNull(),
});

Step 3: Update the SQL query in getBudgetList

Ensure the SQL query in the getBudgetList function correctly sums the amount column.

// File: app/(routes)/dashboard/page.jsx
const getBudgetList = async () => {
  const result = await db
    .select({
      ...getTableColumns(Budgets),
      totalSpend: sql`SUM(${Expenses.amount})`.mapWith(Number),  // Ensure this sums the numeric column
      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();
};

How to replicate the bug

  1. Ensure the amount column in the Budgets table is defined as varchar.
  2. Run the application and navigate to the dashboard page.
  3. Trigger the getBudgetList function, which will attempt to sum the amount column.
  4. Observe the runtime error indicating that the sum function does not exist for character varying.

By following these steps, you should encounter the same error, confirming the bug.

Task

User input/request

Ticket title: error

Ticket Description:
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 |

By implementing the above changes, the bug should be resolved, allowing the sum function to operate correctly on the amount columns.

Click here to create a Pull Request with the proposed solution

Files used for this task:

Changes on utils/schema.jsx

Analysis Report

File: utils/schema.jsx

Overview

This file defines the schema for three tables: Budgets, Incomes, and Expenses using the drizzle-orm library. The schema definitions include various columns with different data types.

Schema Definitions

  1. Budgets Table

    • id: Serial (Primary Key)
    • name: Varchar (Not Null)
    • amount: Varchar (Not Null)
    • icon: Varchar
    • createdBy: Varchar (Not Null)
  2. Incomes Table

    • id: Serial (Primary Key)
    • name: Varchar (Not Null)
    • amount: Varchar (Not Null)
    • icon: Varchar
    • createdBy: Varchar (Not Null)
  3. Expenses Table

    • id: Serial (Primary Key)
    • name: Varchar (Not Null)
    • amount: Numeric (Not Null, Default 0)
    • budgetId: Integer (References Budgets.id)
    • createdAt: Varchar (Not Null)

Potential Issue

The error message indicates that there is an issue with the sum function being applied to a column of type character varying (varchar). Specifically, the error is related to the Budgets table.

  • The amount column in the Budgets table is defined as varchar. This is problematic because the sum function in SQL expects a numeric type.

Recommendation

To resolve this issue, the amount column in the Budgets table should be defined as a numeric type instead of varchar. This will ensure that the sum function can be applied without errors.

Suggested Modification

Change the amount column in the Budgets table from varchar to numeric.

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

Conclusion

The root cause of the bug is the incorrect data type for the amount column in the Budgets table. Changing the data type from varchar to numeric should resolve the issue.

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

Bug Analysis Report

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

Issue Description

The error reported is:

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

This error indicates that the sum function is being applied to a column of type character varying (string), which is not supported. The sum function should be applied to a numeric column.

Relevant Code Section

The error occurs in the getBudgetList function:

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 sum function is being applied to Expenses.amount. If Expenses.amount is not a numeric type, this will cause the error.

Next Steps

  1. Verify Column Type: Ensure that the amount column in the Expenses table is of a numeric type (e.g., INTEGER, FLOAT, DECIMAL).
  2. Casting: If the column type cannot be changed, cast the column to a numeric type within the SQL query.

Suggested Code Modification

If the amount column is not numeric, you can cast it to a numeric type in the SQL query:

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 error is likely due to the amount column in the Expenses table being of a non-numeric type. Casting the column to a numeric type in the SQL query should resolve the issue. Further verification of the column type in the schema definition file (utils/schema.jsx) is recommended.

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

Current plan usage: 10.07%

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

<!-- gh-comment-id:2323177851 --> @codeautopilot[bot] commented on GitHub (Sep 1, 2024): # Potential solution The plan to solve the bug involves ensuring that the `amount` columns in both the `Budgets` and `Expenses` tables are of a numeric type. This will allow the `sum` function to operate correctly without causing a runtime error. The reasoning behind this solution is based on the error message indicating that the `sum` function does not exist for `character varying` types, which suggests that the `amount` columns are currently defined as `varchar`. # What is causing this bug? The bug is caused by the `amount` column in the `Budgets` table being defined as `varchar` instead of a numeric type. This causes the `sum` function to fail because it expects a numeric type. Additionally, there might be a similar issue with the `amount` column in the `Expenses` table, although it is already defined as `numeric`. Ensuring both columns are numeric will resolve the issue. # Code ### Step 1: Modify the `Budgets` table schema Change the `amount` column in the `Budgets` table from `varchar` to `numeric`. ```javascript // File: utils/schema.jsx export const Budgets = pgTable("budgets", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull(), // Changed from varchar to numeric icon: varchar("icon"), createdBy: varchar("createdBy").notNull(), }); ``` ### Step 2: Ensure the `Expenses` table schema is correct Verify that the `amount` column in the `Expenses` table is already defined as `numeric`. ```javascript // File: utils/schema.jsx export const Expenses = pgTable("expenses", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull(), // Ensure this is numeric budgetId: integer("budgetId").references(() => Budgets.id), createdAt: varchar("createdAt").notNull(), }); ``` ### Step 3: Update the SQL query in `getBudgetList` Ensure the SQL query in the `getBudgetList` function correctly sums the `amount` column. ```javascript // File: app/(routes)/dashboard/page.jsx const getBudgetList = async () => { const result = await db .select({ ...getTableColumns(Budgets), totalSpend: sql`SUM(${Expenses.amount})`.mapWith(Number), // Ensure this sums the numeric column 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(); }; ``` # How to replicate the bug 1. Ensure the `amount` column in the `Budgets` table is defined as `varchar`. 2. Run the application and navigate to the dashboard page. 3. Trigger the `getBudgetList` function, which will attempt to sum the `amount` column. 4. Observe the runtime error indicating that the `sum` function does not exist for `character varying`. By following these steps, you should encounter the same error, confirming the bug. # Task ## User input/request Ticket title: error Ticket Description: 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 | By implementing the above changes, the bug should be resolved, allowing the `sum` function to operate correctly on the `amount` columns. [Click here to create a Pull Request with the proposed solution](https://api.codeautopilot.com/create-pr?task=30728&hash=c7ee24a2dae2573483f76e884d295ac3d7e8bf3bc2f1d2505eea9a1cc4551121) **Files used for this task:** <details> <summary>Changes on utils/schema.jsx</summary> ## Analysis Report ### File: `utils/schema.jsx` #### Overview This file defines the schema for three tables: `Budgets`, `Incomes`, and `Expenses` using the `drizzle-orm` library. The schema definitions include various columns with different data types. #### Schema Definitions 1. **Budgets Table** - `id`: Serial (Primary Key) - `name`: Varchar (Not Null) - `amount`: Varchar (Not Null) - `icon`: Varchar - `createdBy`: Varchar (Not Null) 2. **Incomes Table** - `id`: Serial (Primary Key) - `name`: Varchar (Not Null) - `amount`: Varchar (Not Null) - `icon`: Varchar - `createdBy`: Varchar (Not Null) 3. **Expenses Table** - `id`: Serial (Primary Key) - `name`: Varchar (Not Null) - `amount`: Numeric (Not Null, Default 0) - `budgetId`: Integer (References `Budgets.id`) - `createdAt`: Varchar (Not Null) #### Potential Issue The error message indicates that there is an issue with the `sum` function being applied to a column of type `character varying` (varchar). Specifically, the error is related to the `Budgets` table. - The `amount` column in the `Budgets` table is defined as `varchar`. This is problematic because the `sum` function in SQL expects a numeric type. #### Recommendation To resolve this issue, the `amount` column in the `Budgets` table should be defined as a numeric type instead of `varchar`. This will ensure that the `sum` function can be applied without errors. ### Suggested Modification Change the `amount` column in the `Budgets` table from `varchar` to `numeric`. ```javascript export const Budgets = pgTable("budgets", { id: serial("id").primaryKey(), name: varchar("name").notNull(), amount: numeric("amount").notNull(), // Changed from varchar to numeric icon: varchar("icon"), createdBy: varchar("createdBy").notNull(), }); ``` ### Conclusion The root cause of the bug is the incorrect data type for the `amount` column in the `Budgets` table. Changing the data type from `varchar` to `numeric` should resolve the issue. </details> <details> <summary>Changes on app/(routes)/dashboard/page.jsx</summary> ## Bug Analysis Report ### File: `app/(routes)/dashboard/page.jsx` #### Issue Description The error reported is: ``` Unhandled Runtime Error NeonDbError: function sum(character varying) does not exist ``` This error indicates that the `sum` function is being applied to a column of type `character varying` (string), which is not supported. The `sum` function should be applied to a numeric column. #### Relevant Code Section The error occurs in the `getBudgetList` function: ```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 `sum` function is being applied to `Expenses.amount`. If `Expenses.amount` is not a numeric type, this will cause the error. #### Next Steps 1. **Verify Column Type**: Ensure that the `amount` column in the `Expenses` table is of a numeric type (e.g., `INTEGER`, `FLOAT`, `DECIMAL`). 2. **Casting**: If the column type cannot be changed, cast the column to a numeric type within the SQL query. #### Suggested Code Modification If the `amount` column is not numeric, you can cast it to a numeric type in the SQL query: ```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 error is likely due to the `amount` column in the `Expenses` table being of a non-numeric type. Casting the column to a numeric type in the SQL query should resolve the issue. Further verification of the column type in the schema definition file (`utils/schema.jsx`) is recommended. </details> <footer> _Disclaimer: This comment was entirely generated using AI. Be aware that the information provided may be incorrect._ Current plan usage: 10.07% **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#4
No description provided.