python - Django and Postgresql operator does not exist: integer = character varying -


i have these 2 models:

class cachedrecord(models.model):     recordname = models.charfield(max_length=100,primary_key=true)     recordcount = models.integerfield()     def __unicode__(self):         return self.recordname  class cachedrecorddata(models.model):     record = models.foreignkey(cachedrecord)     data = models.charfield(max_length=100)     def __unicode__(self):         return self.data 

when try delete cachedrecord admin panel errror:

programmingerror @ /admin/myapp/cachedrecord/  operator not exist: integer = character varying line 1: ...on ( "myapp_cachedrecorddata"."record_id" = "myapp...                                                              ^ hint:  no operator matches given name , argument type(s). might need add explicit type casts. 

i have found many questions (so might duplicate), don't understand answer.

heroku, postgresql, django, comments, tastypie: no operator matches given name , argument type(s). might need add explicit type casts

no operator matches given name , argument type(s). might need add explicit type casts. -- netbeans, postgresql 8.4 , glassfish

where need add these castings in django?

you have set character field (recordname) primary key cachedrecord.

django created automatic primary key (of type integer) cachedrecorddata called id - since there no primary key specified in model definition.

now when try delete cachedrecord, django creating primary key lookup make sure related cachedrecorddata instances deleted, , since 1 key character , other integer - database giving error.

the easiest way solve problem remove primary_key=true recordname , let django manage keys correctly. can add index on column, or other constraints (for example, set unique=true).

you have similar problem here:

def __unicode__(self):     return self.recordname+":"+self.recordcount 

you adding string ':' self.recordcount result in typeerror exception, cannot combine string number:

>>> ':'+3 traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: cannot concatenate 'str' , 'int' objects 

to solve issue:

def __unicode__(self):     return u'{}:{}'.format(self.recordname, self.recordcount) 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -