'SQL runs on localhost but throws SQLSTATE[42883] error on deployment to heroku

i have a laravel/vuejs project i just deployed to heroku. Everything works well both on local and production except for a query that is throwing up SQLSTATE[42883] error on production. The sql query retrieves a count of models created every week:

public function getProductWeeklyData(){
    $products= Product::selectRaw('COUNT(*) AS product_count')
            ->selectRaw('FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7)) AS week_starting')
            ->groupBy('week_starting')
            ->orderBy('week_starting')
            ->take(10)->get();
    $products->each->setAppends([]);

    return response()->json($products, 200);
}

This works on localhost and return an array like so:

0: {product_count: 7, week_starting: "2021-10-31"}
1: {product_count: 12, week_starting: "2021-11-07"}
2: {product_count: 15, week_starting: "2021-11-14"}

which was exactly what i wanted. However, after deploying to heroku and changing the database connection to PostgreSQL, the query fails and start returning this error.

"message": "SQLSTATE[42883]: Undefined function: 7 ERROR: function to_days(timestamp without time zone) does not exist\nLINE 1: select COUNT() AS product_count,FROM_DAYS(TO_DAYS(created_at...\n ^\nHINT: No function matches the given name and argument types. You might need to add explicit type casts. (SQL: select COUNT() AS product_count, FROM_DAYS(TO_DAYS(created_at) -MOD(TO_DAYS(created_at) -1, 7)) AS week_starting from "products" group by "week_starting" order by "week_starting" asc limit 10)"

i tried casting the date like was suggested in the error by doing this

...(created_at::text, 'YYYY-MM-DD') but still got an error of invalid query. How do i get this query working with PostgreSQL on production?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source