Column "now" does not exist postgres django

I have a django app that store a date column in a model/table in postgres first_subscribed = models.DateField(auto_now=True), and it works fine.

But if I enable a trigger to copy data from the table/model to another table. I get : psycopg2.errors.UndefinedColumn: column "now" does not exist .

The log from the server:

flag4
2025-03-13T18:34:03.451065+00:00 app[web.1]: Internal Server Error: /subscriptions/stripe_webhook/
2025-03-13T18:34:03.451067+00:00 app[web.1]: Traceback (most recent call last):
2025-03-13T18:34:03.451068+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
2025-03-13T18:34:03.451068+00:00 app[web.1]: return self.cursor.execute(sql, params)
2025-03-13T18:34:03.451068+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451068+00:00 app[web.1]: psycopg2.errors.UndefinedColumn: column "now" does not exist
2025-03-13T18:34:03.451069+00:00 app[web.1]: LINE 1: v_date_ini := now::date
2025-03-13T18:34:03.451069+00:00 app[web.1]: ^
2025-03-13T18:34:03.451069+00:00 app[web.1]: QUERY:  v_date_ini := now::date
2025-03-13T18:34:03.451070+00:00 app[web.1]: CONTEXT:  PL/pgSQL function tg_fun_name() line 13 at assignment
2025-03-13T18:34:03.451071+00:00 app[web.1]: The above exception was the direct cause of the following exception:    2025-03-13T18:34:03.451071+00:00 app[web.1]:
2025-03-13T18:34:03.451071+00:00 app[web.1]: Traceback (most recent call last):
2025-03-13T18:34:03.451072+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/core/handlers/exception.py", line 55, in inner
2025-03-13T18:34:03.451072+00:00 app[web.1]: response = get_response(request)
2025-03-13T18:34:03.451072+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451073+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/core/handlers/base.py", line 197, in _get_response
2025-03-13T18:34:03.451074+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2025-03-13T18:34:03.451074+00:00 app[web.1]:

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451074+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/views/decorators/http.py", line 64, in inner
2025-03-13T18:34:03.451074+00:00 app[web.1]: return func(request, *args, **kwargs)
2025-03-13T18:34:03.451075+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451075+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/views/decorators/csrf.py", line 65, in _view_wrapper
2025-03-13T18:34:03.451075+00:00 app[web.1]: return view_func(request, *args, **kwargs)
2025-03-13T18:34:03.451075+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451075+00:00 app[web.1]: File "/app/subscriptions/views.py", line 265, in stripe_webhook
2025-03-13T18:34:03.451075+00:00 app[web.1]:     _handle_successful_checkout(checkout_session_user, checkout_session.metadata.beneficiary_id, checkout_session)
2025-03-13T18:34:03.451076+00:00 app[web.1]: File "/app/subscriptions/webhook_utils.py", line 44, in _handle_successful_checkout
2025-03-13T18:34:03.451082+00:00 app[web.1]: Subscription.objects.create(
2025-03-13T18:34:03.451082+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
2025-03-13T18:34:03.451082+00:00 app[web.1]: return getattr(self.get_queryset(), name)(*args, **kwargs)
2025-03-13T18:34:03.451082+00:00 app[web.1]: 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451083+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/query.py", line 679, in create
2025-03-13T18:34:03.451083+00:00 app[web.1]: obj.save(force_insert=True, using=self.db)
2025-03-13T18:34:03.451083+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/base.py", line 891, in save
2025-03-13T18:34:03.451083+00:00 app[web.1]: self.save_base(
2025-03-13T18:34:03.451084+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/base.py", line 997, in save_base
2025-03-13T18:34:03.451084+00:00 app[web.1]: updated = self._save_table(
2025-03-13T18:34:03.451084+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451084+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/base.py", line 1160, in _save_table
2025-03-13T18:34:03.451084+00:00 app[web.1]: results = self._do_insert(
2025-03-13T18:34:03.451084+00:00 app[web.1]: ^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451085+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/base.py", line 1201, in _do_insert
2025-03-13T18:34:03.451085+00:00 app[web.1]: return manager._insert(
2025-03-13T18:34:03.451085+00:00 app[web.1]: ^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451085+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/manager.py", line 87, in manager_method
2025-03-13T18:34:03.451085+00:00 app[web.1]: return getattr(self.get_queryset(), name)(*args, **kwargs)
2025-03-13T18:34:03.451085+00:00 app[web.1]: 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451085+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/query.py", line 1847, in _insert
2025-03-13T18:34:03.451086+00:00 app[web.1]: return query.get_compiler(using=using).execute_sql(returning_fields)
2025-03-13T18:34:03.451086+00:00 app[web.1]: 

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451086+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1836, in execute_sql
2025-03-13T18:34:03.451086+00:00 app[web.1]: cursor.execute(sql, params)
2025-03-13T18:34:03.451086+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/backends/utils.py", line 122, in execute
2025-03-13T18:34:03.451086+00:00 app[web.1]: return super().execute(sql, params)
2025-03-13T18:34:03.451087+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451087+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/backends/utils.py", line 79, in execute
2025-03-13T18:34:03.451087+00:00 app[web.1]: return self._execute_with_wrappers(
2025-03-13T18:34:03.451087+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451087+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
2025-03-13T18:34:03.451087+00:00 app[web.1]: return executor(sql, params, many, context)
2025-03-13T18:34:03.451087+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451099+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/backends/utils.py", line 100, in _execute
2025-03-13T18:34:03.451099+00:00 app[web.1]: with self.db.wrap_database_errors:
2025-03-13T18:34:03.451099+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451100+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/utils.py", line 91, in __exit__
2025-03-13T18:34:03.451100+00:00 app[web.1]: raise dj_exc_value.with_traceback(traceback) from exc_value
2025-03-13T18:34:03.451100+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.12/site-packages/django/db/backends/utils.py", line 105, in _execute
2025-03-13T18:34:03.451100+00:00 app[web.1]: return self.cursor.execute(sql, params)
2025-03-13T18:34:03.451100+00:00 app[web.1]: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-03-13T18:34:03.451101+00:00 app[web.1]: django.db.utils.ProgrammingError: column "now" does not exist    2025-03-13T18:34:03.451101+00:00 app[web.1]: LINE 1: v_vigencia_ini := now::date
2025-03-13T18:34:03.451101+00:00 app[web.1]: ^
2025-03-13T18:34:03.451101+00:00 app[web.1]: QUERY:  v_vigencia_ini := now::date
2025-03-13T18:34:03.451101+00:00 app[web.1]: CONTEXT:  PL/pgSQL function tg_fun_name() line 13 at assignment

It is like the trigger was capturing the new values as now::date and that creates an error on postgres. But if I disable the trigger it works fine.

Please, Can anyone help me understand and fix this?

In Postgres, "now" is actually represented as a function now(). In order to get the current timestamp, you'll need to do something like v_vigencia_ini := now()::date

If you update your trigger definition, the error should go away.

Additionally, if you would like the current date, you can do that without having to call now(), and thereby no need to cast with ::date

edb_admin=> select CURRENT_DATE;
 current_date 
--------------
 2025-03-13
(1 row)

Therefore, you can update your trigger definition with v_vigencia_ini := CURRENT_DATE as well

From the name of the field, I though you meant to want auto_now_add not auto_now so that it takes now date only when creating the row (not when updating the row). However, although this might solve your problem, but I can't understand why your trigger did not work fine.. please can you share your tg_fun_name function difinition.

Back to Top