Open MuskanNazim opened 2 months ago
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
.
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.
Budgets
table schemaChange 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(),
});
Expenses
table schema is correctVerify 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(),
});
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();
};
amount
column in the Budgets
table is defined as varchar
.getBudgetList
function, which will attempt to sum the amount
column.sum
function does not exist for character varying
.By following these steps, you should encounter the same error, confirming the bug.
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:
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 () => {