#29569: AutoField.cast_db_type returns the wrong 'type' for PostgrestSQL -------------------------------------+------------------------------------- Reporter: Andrew | Owner: nobody Standley | Type: Bug | Status: new Component: Database | Version: 2.0 layer (models, ORM) | Severity: Normal | Keywords: cast Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- Using PostgreSQL 10.1 I encountered a problem trying to use `Cast` with an `AutoField`. Trying to perform a manual join on generic foreign keys: `Item.objects.filter(pk__in=Activity.objects.filter(content_type=item_ct).annotate(casted_pk=Cast('object_pk', AutoField())).values('casted_pk'))` The db connection responds with a "psycopg2.ProgrammingError: type "serial" does not exist" error.
Upon investigation, it seems that `AutoField.cast_db_type` uses the inherited definition from `Field` and returns `AutoField.db_type`. However `AutoField.db_type` returns 'serial', which is a "syntactical sugar" (not a true type) in Postgresql and only valid for creation. The issue arises when `AutoField.cast_db_type` also returns 'serial', which is invalid. More confusingly `AutoField.rel_db_type` has already been overridden and correctly returns 'integer'. I believe fixing this is as easy as copying the override of `AutoField.rel_db_type` to `AutoField.cast_db_type`. I'll try to at least get this implemented as a test on my fork, so that it is easy to confirm, but in the mean time the models to reproduce are: {{{ class Item(models.Model): label = models.CharField(max_length=100) cost = models.IntegerField() class Activity(models.Model): content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_pk = models.CharField(max_length=100) generic_object = GenericForeignKey(fk_field='object_pk') }}} Reference: PostgreSQL docs on 'serial' type (https://www.postgresql.org/docs/10/static/datatype-numeric.html#DATATYPE- SERIAL) -- Ticket URL: <https://code.djangoproject.com/ticket/29569> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/054.06466e30bdd4caf0db1f1c8b9b90b5cc%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.