`Model.objects.create()` returns `long` instead of `int`.
|Reported by:||Tai Lee||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
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 6 years ago by
|Patch needs improvement:||set|
comment:9 Changed 5 years ago by
|Status:||new → closed|
|Triage Stage:||Ready for checkin → Design decision needed|