Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#927 closed defect (fixed)

non-integer primary keys are broken since new-admin merge

Reported by: L.Plant.98@… Owned by: Adrian Holovaty
Component: Metasystem Version:
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


From "svn annotate django/core/meta/", line 721 onwards:

  1434     adrian     def get_db_prep_save(self,value):
  1434     adrian         if value == '' or value == None:
  1434     adrian            return None
  1434     adrian         else:
  1434     adrian            return int(value)

That "return int(value)" obviously is not compatible with non-integer primary keys. The problem occurs outside of the admin - i.e. when creating objects and setting foreign keys manually in my migration script.

Change History (8)

comment:1 Changed 12 years ago by rjwittams

So the right fix here needs to take the rel.get_related_field() into account, as it will have the same type of value as that. Maybe delegate to the db_prep_save of that field. But it also needs to take related fields being null when the field it is related to cannot be.

comment:2 Changed 12 years ago by hugo

#968 and #1010 where duplicates and might include additional information.

comment:3 Changed 12 years ago by hugo

#963 it was, sorry ...

comment:4 Changed 12 years ago by anonymous

It appears that the to_field in the FK cannot be non-integer.

comment:5 Changed 12 years ago by anonymous

As per rjwittams suggestion, I have changed my local copy to:

def get_db_prep_save(self,value):

return self.rel.get_related_field().get_db_prep_save(value)

And of course it does work, now. About the issue with fields being null, wouldn't the delegated method for the model field
check that automatically? Pardon my ignorance, as I am just begining to dig into the internals.

comment:6 Changed 12 years ago by rjwittams

Hey anonymous,

The point is that a ForeignKey field could be null, so that needs to be an accepted value. Any primary key cannot be null, so would always complain.

def get_db_prep_save(self,value):
    if value == '' or value == None:
        return None
        return self.rel.get_related_field().get_db_prep_save(value)

is probably the right thing for now.

comment:7 Changed 12 years ago by anonymous

I use custom alphanumeric pks and rjwittams fix worked for me. Thanks!

comment:8 Changed 12 years ago by Adrian Holovaty

Resolution: fixed
Status: newclosed

(In [1569]) Fixed #927 -- Non-integer primary keys save() method now works

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