Opened 8 years ago

Closed 5 years ago

#7783 closed Bug (fixed)

PostgreSQL database introspection does not detect NULL columns

Reported by: bthomas@… Owned by: nobody
Component: Core (Management commands) Version: master
Severity: Normal Keywords: inspectdb
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

cursor.description does not return the null_ok field, so database introspection does not add null=True to column definitions. The is_nullable column in information_schema.columns does contain this information, however.

Attachments (3)

pgnulls.diff (1022 bytes) - added by bthomas@… 8 years ago.
Patch to map the is_nullable column from information_schema to the null_ok item in cursor.description
pgnulls_django1.diff (689 bytes) - added by ricardodani 5 years ago.
Same patch, but for django >= 1
7783-3.diff (2.6 KB) - added by Claude Paroz 5 years ago.
Patch updated with test

Download all attachments as: .zip

Change History (18)

Changed 8 years ago by bthomas@…

Attachment: pgnulls.diff added

Patch to map the is_nullable column from information_schema to the null_ok item in cursor.description

comment:1 Changed 8 years ago by Malcolm Tredinnick

Is this valid for both postgreSQL 7.x and 8.x? We support both.

comment:2 Changed 8 years ago by Bob Thomas <bthomas@…>

I am only using PostgreSQL 8.x, but I think information_schema was added in PostgreSQL 7.4:

http://www.postgresql.org/docs/7.4/static/information-schema.html

comment:3 Changed 8 years ago by Simon Greenhill

Patch needs improvement: set
Triage Stage: UnreviewedAccepted

Accepted. Can someone confirm if/what versions of 7.x this works on?

comment:4 Changed 6 years ago by Gabriel Hurley

Component: django-admin.py inspectdbCore (Management commands)

comment:5 Changed 6 years ago by Peter Baumgartner

Severity: Normal
Type: Bug

comment:6 Changed 5 years ago by anonymous

Easy pickings: unset
UI/UX: unset

We tried to setup an environment using PostgreSQL 7.4.30 (the oldest one available for download)[1] to answer your question. Unfortunately when we run manage.py syncdb we received an error message stating that:

"django.db.utils.DatabaseError: function pg_get_serial_sequence("unknown", "unknown") does not exist"

Further research in the django-users mailing list revealed that version 7.4 of PostgreSQL does not contain the function pg_get_serial_sequence.[2] A reply from django core developer Russell Keith-Magee says that in Django version 1.3 support for PostgreSQL 7.4 was abandoned.[3]

[1]http://www.postgresql.org/ftp/source/
[2]http://markmail.org/thread/pf4bmphpsv6m4djl
[3]http://markmail.org/message/tp5czlz3ipy7vd5j

comment:7 Changed 5 years ago by anonymous

Triage Stage: AcceptedReady for checkin

comment:8 Changed 5 years ago by Aymeric Augustin

Needs tests: set

This can't be RFC, the patch doesn't even apply:

trunk % patch -p0 < ~/Downloads/pgnulls.diff
patching file django/db/backends/postgresql_psycopg2/introspection.py
Hunk #1 FAILED at 15.
1 out of 1 hunk FAILED -- saving rejects to file django/db/backends/postgresql_psycopg2/introspection.py.rej

comment:9 Changed 5 years ago by Aymeric Augustin

Triage Stage: Ready for checkinAccepted

comment:10 Changed 5 years ago by ricardodani

This patch is applied to which version? I have the same problem, but using django 1.3 and postgres_psycopg2 driver and this patch can not be applied, just like aaugustin have reported.

Changed 5 years ago by ricardodani

Attachment: pgnulls_django1.diff added

Same patch, but for django >= 1

comment:11 Changed 5 years ago by ricardodani

Added patch to version of django > 1.0

comment:12 Changed 5 years ago by Ramiro Morales

Keywords: inspectdb added

Changed 5 years ago by Claude Paroz

Attachment: 7783-3.diff added

Patch updated with test

comment:13 Changed 5 years ago by Claude Paroz

Needs tests: unset
Patch needs improvement: unset

comment:14 Changed 5 years ago by Ramiro Morales

Triage Stage: AcceptedReady for checkin

comment:15 Changed 5 years ago by Ramiro Morales

Resolution: fixed
Status: newclosed

In [17508]:

Fixed #7783 -- Made introspection of nullable columns more robust with Postgres.

Thanks bthomas AT ncorcle DOT com for the report and initial patch, and
Claude Paroz for the final, complete patch.

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