abhishek-ram / pyas2

This repo is no longer maintained and has been moved to https://github.com/abhishek-ram/django-pyas2
GNU General Public License v2.0
47 stars 23 forks source link

Getting Duplicate PRIMARY key error #40

Closed whippersnapper closed 7 years ago

whippersnapper commented 7 years ago

Can anyone help to explain why the server throws exception on a particular inbound receive? Someone is sending to us a X12 formatted payload. We are getting the following exception. Seems to be duplicate key? I'm not quite sure why this is happening.

20170921 12:52:35 ERROR    : Fatal error while processing message AS2.11.BizLink.3.3.0.68.668c2956-4fa2-460e-8626-ca138278ccb4.4388672f-9eed-11e7-a4c6-c6b9043a942e@85aa9ee9f8d90158.5c761060.15ea55aeec1.8000, error:
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/pyas2/views.py", line 544, in as2receive
    status_message=status_message)
  File "/usr/lib/python2.7/site-packages/pyas2/as2lib.py", line 311, in build_mdn
    message.save()
  File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 796, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 824, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 908, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/usr/lib64/python2.7/site-packages/django/db/models/base.py", line 947, in _do_insert
    using=using, raw=raw)
  File "/usr/lib64/python2.7/site-packages/django/db/models/manager.py", line 85, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/usr/lib64/python2.7/site-packages/django/db/models/query.py", line 1045, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/usr/lib64/python2.7/site-packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
    cursor.execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/lib64/python2.7/site-packages/django/db/backends/mysql/base.py", line 110, in execute
    return self.cursor.execute(query, args)
  File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
IntegrityError: (1062, "Duplicate entry '.... the message id that is scrubbed here ....' for key 'PRIMARY'")
abhishek-ram commented 7 years ago

That is a strange error since I am catching these, can you search for messages or mdns with that message ID?

whippersnapper commented 7 years ago

@abhishek-ram it never made to the MDN sends. There is no messages generated. If the save would of gone through, then I believe that is the next step (generating the MDN).

whippersnapper commented 7 years ago

I wanted to note that the transfer encoding is content-transfer-encoding: binary is this supported?

whippersnapper commented 7 years ago

I saw that in the source code, you've made a comment about composite key? I wonder if that would help?

abhishek-ram commented 7 years ago

Yeah the composite key would help, but you are saying that you are not even able to find another message with this ID so I am not sure what issue this is. Yes the binary encoding is supported.

whippersnapper commented 7 years ago

I figured out what the problem was. The incoming message id surpassed the model max_length settings (currently set to 100). The incoming partner's message was over that. Django seems to just auto trucate by default and carry on. That caused some weird behaviors on the save() as it tries to insert again vice upsert.

abhishek-ram commented 7 years ago

Okay should i go ahead and increase the size and release a patch?