Changes between Version 10 and Version 11 of Ticket #35941


Ignore:
Timestamp:
Dec 2, 2024, 3:29:59 AM (5 weeks ago)
Author:
Csirmaz Bendegúz
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #35941 – Description

    v10 v11  
    66
    771. `object_id` is a `CharField` (or `TextField`)
    8 2. ''CompositePrimaryKey'' is stored as a ''JSON array'' in `object_id`
     82. ''CompositePrimaryKey'' is stored as a ''JSON array'' in `object_id` e.g. `object_id='[1, "abc"]'`
    993. JOINs can be achieved with JSON functions (varies per db backend)
    1010
     
    2929
    3030JOIN on JSON functions is not efficient
    31 ''When storing a composite primary key in a `CharField` / `TextField`, JSON is the best option we have because it's widely supported by database backends. To achieve better performance, the composite pk shouldn't be stored in a `CharField` / `TextField` in the first place.''
     31''When storing a composite primary key in a `CharField` / `TextField`, JSON is the best option we have because it's widely supported by database backends. To achieve better performance, the composite pk shouldn't be stored in a `CharField` / `TextField` in the first place (see: alternatives).''
    3232
    3333**Notes:**
     
    4141It's possible to implement other strategies to deal with composite generic foreign keys.
    4242
    43 1. ''JSONField'' - instead of `CharField` / `TextField`, we could make "object_id" a `JSONField`. This achieves the same thing as my proposal but saves the `::jsonb` cast.
    44 2. GenericForeignKey with multiple "object_id"s - less flexible, can't store both regular primary keys and composite primary keys.
     431. ''JSONField'' - instead of `CharField` / `TextField`, we could make "object_id" a `JSONField`. This achieves the same thing but saves the `::jsonb` cast, so it's slightly faster.
     442. ''GenericForeignKey with multiple `object_id`s'' - less flexible, can't store both regular primary keys and composite primary keys. Fastest JOINs.
    4545
    46 There's no reason we couldn't implement other strategies. I believe my proposal provides the most flexibility and backwards-compatibility (at the cost of performance).
     46There's no reason we couldn't implement other strategies. I believe my proposal provides the most flexibility at the cost of performance. It should be implemented for backwards-compatibility, so systems using a `CharField` ''object_id'' (e.g. [https://github.com/django-guardian/django-guardian/blob/55beb9893310b243cbd6f578f9665c3e7c76bf96/guardian/models/models.py#L39 django-guardian]) can easily integrate with composite primary keys. Other strategies can then be implemented to improve performance (or other reasons).
    4747
    4848Any feedback is appreciated!
Back to Top