get_or_create created 2 instances [Sloppy Development]

I am a woman enough to talk about this. I usually don’t let these things happen, but this is such a small thing that let me down on a really high amount of requests.

The statistics of an API I was using showed that at 00:00 UTC we stopped using it altogether.

So, the culprit is the simplest model ever:

class Date(models.Model):
    date = DateField()

And I only ever created it (or altered) like this:

    date, cr = Date.objects.get_or_create(
        day=f'{this_moment.year}-{this_moment.month}-{this_moment.day}'

As it always happens, 4am Sunday it well went to hell – Django create two instances of ‘2021-08-15’.

Eventually every requests that had this line in it just crashed.

Why did this happen? Well, apparently two instances are not supposed to be made.

My friends and I came to a conclusion that with a very high amount of requests Django missed that tiny bit of time between executing get_or_create method and actually writing an instance to PostgreSQL.

Well good thing - I went to bed late, and my team woke me up much earlier than I would wake up.

What is he solution? If the field is meant to be unique, make it unique:

class Date(models.Model):
    date = DateField(unique=True)

Django + Esputnik (part 2) – Error: Cannot transform message content

Long story short, something is wrong with your dynamic tags. Two rules need to be applied:

1. Use ‘ but not “

Right version:

$!data.get(‘your_variable’)

Wrong version:

$!data.get("your_variable") 

2. The tag was broken by another tag

Open the HTML editor of the entire message and Ctrl+F $!data.get(‘.

All the tags need to look like this $!data.get(‘something’). BUT! You may see something like this:

$!data.get(‘something' <span><strong>)

And these <span><strong> are what causing the error.