Opened 4 weeks ago

Closed 4 weeks ago

#35928 closed Bug (duplicate)

[contenttypes] GenericRelation uses wrong primary key type if set to models.UUIDField

Reported by: Jon Eyolfson Owned by:
Component: contrib.contenttypes Version: 5.1
Severity: Normal Keywords: GenericRelation, UUIDField
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Jon Eyolfson)

For a minimal example, if I have the following:

class Delivery(models.Model):

    id = models.UUIDField(primary_key=True, editable=False)
    content_type = models.ForeignKey(
        ContentType,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        editable=False,
    )
    object_id = models.PositiveBigIntegerField(
        blank=True,
        null=True,
        editable=False,
    )
    content_object = GenericForeignKey("content_type", "object_id")

class Other(models.Model):

    delivery = GenericRelation(Delivery, related_query_name="other")

If I use delivery.other.all().query I see the wrong primary key:

SELECT /* SNIP */ WHERE "delivery"."id" = 269290806699601176541887178676879701060

If I look at a normal query with the primary key on Delivery, like Delivery.objects.filter(pk=delivery_pk).query, I see:

SELECT /* SNIP */ WHERE "delivery"."id" = ca9785d8a6e311ef874b0ad61d0b3444

This causes SQLite to show the following error:

OverflowError: Python int too large to convert to SQLite INTEGER

I assume because GenericRelation is converting the UUID to an integer, which would cause the lookup to fail anyways.

Change History (2)

comment:1 by Jon Eyolfson, 4 weeks ago

Description: modified (diff)

comment:2 by Simon Charette, 4 weeks ago

Resolution: duplicate
Status: newclosed

Duplicate of #33450 Integer primary key is wrongly casted to UUID when filtering GenericRelation on model with UUID primary key.

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