`Model.objects.create()` returns `long` instead of `int`.
|Reported by:||mrmachine||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Severity:||Normal||Keywords:||int long primary key create get|
|Cc:||Triage Stage:||Design decision needed|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
Some database backends (I've only tested postgresql, sqlite seems OK) seem to return a long PK instead of int when creating new model objects. When getting an existing model object, int is correctly returned. I did find a comment about this in the tutorial, but I don't think this is by design and I think this difference in behaviour between new and existing objects and database backends is buggy and violates the principle of least surprise.
It might be a bit of an edge case, but we were stumped for a while trying to figure out why created objects had a PK of a different type to existing objects. We initially noticed the problem because we use a pickle field to store various types of data -- PKs, dates and times, booleans, dicts, etc. -- and when passing in the value of a newly created PK to the pickle field we were then unable to filter on it unless we explicitly searched for long values.
So to work-around this issue we need to a) know the type of the PK that should be returned for existing objects and coerce it before passing to the pickle field if it does not match.
Change History (11)
comment:1 Changed 5 years ago by mrmachine
- Has patch set
- Needs documentation unset
- Needs tests unset
- Patch needs improvement set
Changed 5 years ago by mrmachine
comment:8 Changed 5 years ago by dmclain
- Easy pickings unset
- Triage Stage changed from Accepted to Ready for checkin
comment:9 Changed 5 years ago by mtredinnick
- Resolution set to wontfix
- Status changed from new to closed
- Triage Stage changed from Ready for checkin to Design decision needed
- UI/UX unset