Opened 113 minutes ago
#35941 new New feature
Add composite GenericForeignKey support
Reported by: | Csirmaz Bendegúz | Owned by: | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Csirmaz Bendegúz | Triage Stage: | Unreviewed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This is a follow up to #373 (CompositePrimaryKey).
Proposal:
My proposal is to implement GenericForeignKey support with JSON.
object_id
is aCharField
(orTextField
)- CompositePrimaryKey is stored as a JSON array in
object_id
- JOINs can be achieved with JSON functions (varies per db backend)
If anyone is interested, I have a proof of concept available on my GitHub (it's an out of sync draft right now, I'll submit a proper PR once composite primary keys are merged).
Risks:
- What if someone is using a JSON array as the primary key (but it's not a composite primary key)?
Before deserializing the JSON array, we need to check if the content type has a composite primary key or not.
- What if the db backend doesn't support JSON functions?
All supported databases support JSON functions.
Notes:
Django admin's LogEntry
has its own implementation of "generic foreign keys".
The approach we take with GenericForeignKey
should also apply to LogEntry
.