sql server - Django - OneToOneField can be Null? -
i'm trying fetch messages 365 api , save them on initialmessage model. when ever try so, keep getting message:
django.db.utils.integrityerror: ('23000', "[23000] [microsoft][sql server native client 11.0][sql server]violation of unique key constraint 'uq__assignme__f9cc0897dc370444'. cannot insert duplicate key in object 'dbo.assignment_initialmessage'. duplicate key value (). (2627) (sqlparamdata)")
unique key constraint refering relationship model claim (claim = models.onetoonefield(claim, null=true, blank=true, default=none)
this model:
class initialmessage(models.model): json = models.textfield() att_url = models.charfield(max_length = 254, default='https://outlook.office365.com/api/v1.0/me/messages/{0}/attachments') send_url = models.charfield(max_length = 254, default='https://outlook.office365.com/api/v1.0/me/sendmail') update_url = models.charfield(max_length = 254, default='https://outlook.office365.com/api/v1.0/me/messages/{0}') id_email = models.charfield(max_length = 254,unique=false) subject = models.textfield() body_content = models.textfield() body_type = models.textfield() importance = models.charfield(max_length = 254) hasattachments = models.booleanfield(default=false) parentfolderid = models.charfield(max_length = 254) e_from = models.textfield() sender = models.textfield() conversationid = models.textfield() categories = models.textfield() datetimesent = models.datetimefield() datetimereceived = models.datetimefield() status = models.charfield(max_length=2, choices=initialmessage_status, default='p') claim = models.onetoonefield(claim, null=true, blank=true, default=none) def load_json(self): m_json = json.loads(self.json) try: email = initialmessage.objects.get(id_email = m_json['id']) except initialmessage.doesnotexist: email = none if email none: self.id_email = m_json['id'] self.subject = m_json['subject'] self.body_content = m_json['body']['content'] self.body_type = m_json['body']['contenttype'] self.importance = m_json['importance'] self.hasattachments = m_json['hasattachments'] self.parentfolderid = m_json['parentfolderid'] self.e_from = m_json['from'] self.sender = m_json['sender'] self.conversationid = m_json['conversationid'] self.categories = m_json['categories'] self.datetimesent = m_json['datetimesent'] self.datetimereceived = m_json['datetimereceived'] self.save() else: m_json = 'duplicated' return m_json
and code i'm using save each new email:
log.debug('fetching messages.') response = requests.get(self.inbox_url,auth=self.auth,params={'$filter':self.filters}) log.info('response o365: %s', str(response)) message in response.json()['value']: m = initialmessage(json = json.dumps(message)).load_json() log.debug('all messages retrieved , put in list.') return true
have ideas? help!
Comments
Post a Comment