In a django view, I need to append string data to the end of an existing text column in my database. So, for example, say I have a table named "ATable", and it has a field named "aField". I'd like to be able to append a string to the end of "aField" in a race-condition-free way. Initially, I had this:
tableEntry = ATable.objects.get(id=100)
tableEntry.aField += aStringVar
tableEntry.save()
The problem is that if this is being executed concurrently, both can get the same "tableEntry", then they each independently update, and the last one to "save" wins, losing the data appended by the other.
I looked into this a bit and found this, which I hoped would work, using an F expression:
ATable.objects.filter(id=100).update(aField=F('aField') + aStringVar)
The problem here, is I get an SQL error, saying:
operator does not exist: text + unknown
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
Tried changing to "str(aStringVar)" even though its already a string - no luck.. I found a couple django bug reports complaining about similar issues, but I didn't see a fix or a workaround. Is there some way I can cast aStringVar such that it can be appended to the text of the F expression? BTW - also tried "str(F('aField')) + aStringVar" but that converted the result of the F expression to the string "(DEFAULT: )".