We are using the Flask development server which I know we need to change. It seems that when two requests come in very close to each other bad things happen. On one of those occasions we got this sqlalchemy.exc.InvalidRequestError upon commit.
The response object had already been created and assigned a UUID:
class ServiceResponse(db.Model):
service_response_id = db.Column(
db.String(32),
unique=True,
nullable=False,
primary_key=True,
default=lambda: uuid.uuid4().hex
)
status_code = db.Column(db.SmallInteger, nullable=False)
The problem is that all subsequent requests kept assigning the same UUID to the corresponding response object. This caused integrity errors because a response object with the same UUID already existed in the DB. After I deleted the offending row from the DB the same value was generated one more time, didn't cause an integrity error anymore, and since then different values have been generated as expected.
I'm aware that the type could be changed to UUID but my question is why is uuid.uuid4() generating the same value over and over again for different requests?