# CONCAT to update db text field

I needed to collect messages from different processes which was triggered by a record insertion. To keep things simple and denormalized, this stack overflow [gem](https://stackoverflow.com/questions/3300944/can-i-use-django-f-objects-with-string-concatenation) allows for that.

```python
from django.db import models

class DBModel(models.Model):
    @classmethod
    def append_text(cls, event_id, text_field, message)
        cls.objects.filter(id=event_id).update(
            text_field=Concat(text_field, Value(message))
        )
```

Using the database function `CONCAT`, it offloads [serializability](https://dev.mysql.com/doc/refman/8.4/en/innodb-transaction-isolation-levels.html) to the database guaranteeing no race conditions.

> `Concat(ConcatPair(F(text_field), Value(message)))`

Using `F` methods, it simply appends `message` to the existing field.

Happy hackin’!

Reference:

* [Can I use Django F() objects with string concatenation?](https://stackoverflow.com/questions/3300944/can-i-use-django-f-objects-with-string-concatenation)
    
* [MySQL: Concat](https://dev.mysql.com/doc/refman/9.0/en/string-functions.html#function_concat)
    
* [MySQL: Isolation levels](https://dev.mysql.com/doc/refman/8.4/en/innodb-transaction-isolation-levels.html)
