Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31400 closed Bug (fixed)

base.py returns TypeError when CONN_MAX_AGE is set in a Postgresql connection

Reported by: danb4r Owned by: Hasan Ramezani
Component: Documentation Version: 3.0
Severity: Normal Keywords: CONN_MAX_AGE, base.py, Postgresql
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

Just did a clean Django 3.0.4
Python 3.6.9, install with
Postgresql Version: 10+190ubuntu0.1 via psycopg2 (2.8.4)
inside Windows Subsystem for Linux.

When I run:

python manage.py migrate

I get:

  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 192, in connect
    self.close_at = None if max_age is None else time.monotonic() + max_age
TypeError: unsupported operand type(s) for +: 'float' and 'str'

The error does not happen with CONN_MAX_AGE not set:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'django_p1_master',
        'USER': 'django',
        'PASSWORD': 'secret',
        'HOST': '127.0.0.1',
        'PORT': '5432',
#        'CONN_MAX_AGE': '600',
    }
}

Full error stack:

(venv-django_p1) vmuser@REDBEAST:/mnt/c/usr/workspace/django_p1/dsite$ python manage.py migrate
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 86, in handle
    executor = MigrationExecutor(connection, self.migration_progress_callback)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/migrations/executor.py", line 18, in __init__
    self.loader = MigrationLoader(self.connection)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/migrations/loader.py", line 49, in __init__
    self.build_graph()
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/migrations/loader.py", line 212, in build_graph
    self.applied_migrations = recorder.applied_migrations()
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 76, in applied_migrations
    if self.has_table():
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/migrations/recorder.py", line 56, in has_table
    return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 260, in cursor
    return self._cursor()
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 236, in _cursor
    self.ensure_connection()
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
    self.connect()
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/utils/asyncio.py", line 26, in inner
    return func(*args, **kwargs)
  File "/mnt/c/usr/workspace/django_p1/venv-django_p1/lib/python3.6/site-packages/django/db/backends/base/base.py", line 192, in connect
    self.close_at = None if max_age is None else time.monotonic() + max_age
TypeError: unsupported operand type(s) for +: 'float' and 'str'

Change History (6)

comment:1 by Simon Charette, 5 years ago

Triage Stage: UnreviewedAccepted

Pretty sure this is due to the fact you use a str ('0') instead of an int (0) to define CONN_MAX_AGE.

Accepting on the basis that the documentation could be adjusted to mention a positive int must be provided instead of the apparently too vague number term.

comment:2 by Simon Charette, 5 years ago

Component: Database layer (models, ORM)Documentation
Easy pickings: set

comment:3 by Hasan Ramezani, 5 years ago

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:4 by Hasan Ramezani, 5 years ago

Has patch: set
Last edited 5 years ago by Mariusz Felisiak (previous) (diff)

comment:5 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

Resolution: fixed
Status: assignedclosed

In bec4dea:

Fixed #31400 -- Doc'd the expected type of CONN_MAX_AGE database option.

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 5 years ago

In a6cc8b80:

[3.0.x] Fixed #31400 -- Doc'd the expected type of CONN_MAX_AGE database option.

Backport of bec4dea844332390aecd72f22afc6673a3f5f5fc from master

Note: See TracTickets for help on using tickets.
Back to Top