Opened 2 years ago

Last modified 2 months ago

#24928 assigned New feature

Add UUIDField, RangeField, JSONField, etc to database introspection

Reported by: Markus Holtermann Owned by: Claude Paroz
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

django.db.backends.postgresql_psycopg2.introspection.DatabaseIntrospection.data_types_reverse does not handle UUIDField and other core or contrib.postgres specific fields. And neither does any of the other backends.

Change History (13)

comment:1 Changed 2 years ago by Tim Graham

Triage Stage: UnreviewedAccepted
Type: BugNew feature

There's an interesting question about how contrib.postgres can "contribute" introspection rules to the postgres backend. Maybe the contrib.postgres fields should be handled in a separate ticket.

comment:2 Changed 2 years ago by Claude Paroz

IMHO, introspection should show the most accurate type, independently of the presence of contrib.postgres in the INSTALLED_APPS.

comment:3 Changed 2 years ago by Tim Graham

Claude, the issue in my mind is adding contrib references to django/db/backends/postgresql_psycopg2/introspection.py. If we allow contrib.postgres to play by special rules, that's not very friendly to third-party apps that might wish to add introspection for their own fields. But you have more experience in this area than I do, so please give your thoughts if you had ideas on how this should work.

comment:4 Changed 2 years ago by Claude Paroz

For me, this is not special rules, it's the rule "Django tries its best to introspect the database fields". If anyone want to provide contrib.mysql or contrib.oracle with custom fields, she's welcome to do so and we'll also be able to improve introspection for those backends.

Currently Django outputs models.TextField(blank=True, null=True) # This field type is a guess. for fields it doesn't know. Even if some field type is not implemented by Django models, we could imagine that we provide better hints (provided the type code is known), and point to popular third-party packages which implements those fields, inside comments.

comment:5 Changed 2 years ago by Tim Graham

What I envisioned for my proposal is a way for any app to contribute a mapping to a particular database backend's DatabaseIntrospection.data_types_reverse dictionary. I guess it might work something like south.modelsinspector.add_introspection_rules does, although I haven't looked at the implementation of that. Of course there's also the need to add the appropriate import in the models file that inspectdb generates since all fields won't be in django.db.models anymore.

comment:6 Changed 16 months ago by Tim Graham

Summary: Add UUIDField, RangeField, etc to database introspectionAdd UUIDField, RangeField, JSONField, etc to database introspection

comment:7 Changed 16 months ago by Claude Paroz

Owner: changed from nobody to Claude Paroz
Status: newassigned

comment:8 Changed 16 months ago by Claude Paroz

As UUIDField is a bit different (available in all backends with different implementation), I created a pull request just for that field.
A separate pull request will be proposed for other PostgreSQL-specific fields.

comment:9 Changed 16 months ago by Claude Paroz

Has patch: set

The "final" pull request, including the commit linked from the previous comment and the fix for #25454.

comment:10 Changed 16 months ago by Claude Paroz <claude@…>

In 3ea7167:

Refs #24928 -- Added introspection support for UUIDField

comment:11 Changed 2 months ago by Claude Paroz <claude@…>

In 0cbb6ac0:

Refs #24928 -- Added introspection support for PostgreSQL JSONField

Thanks Adam Johnson and Tim Graham for the reviews.

comment:12 Changed 2 months ago by Claude Paroz <claude@…>

In a599ae60:

Refs #24928 -- Added introspection support for PostgreSQL RangeField

comment:13 Changed 2 months ago by Claude Paroz

Patch needs improvement: set

HStoreField and ArrayField support still missing.

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