0

I have a Django project with Postgresql on DigitalOcean. Strangely, when I create an object it's triplicated on the remote server and duplicated on the local machine.

Here are my models.

from django.db import models

class Instrument(models.Model):
    id = models.CharField(max_length=4, primary_key=True)

    def __str__(self):
        return self.id

class Quote(models.Model):
    instrument = models.ForeignKey(Instrument, on_delete=models.CASCADE, 
    related_name='quotes')
    quote = models.FloatField()
    timestamp = models.DateTimeField(auto_now_add=True)


    def __str__(self):
        return self.instrument.id;

And this is how I create an object:

q = Quote(instrument=Instrument.objects.get(pk='GBP'), quote=1.2100)
q.save()

I tried creating objects in different ways:

i = Instrument.objects.get(pk='EUR')
q = Quote.objects.create(instrument=i, quote=1.2000)

I would appreciate any comments and ideas how to resolve it.

I use Apscheduler to retreive the data and store it in DB and I have 3 records in DB at each execution:

    {
        "instrument": "iEUR",
        "quote": 2.19572761275879,
        "timestamp": "2019-08-26T23:09:00.540737Z"
    },
    {
        "instrument": "iEUR",
        "quote": 2.19572761275879,
        "timestamp": "2019-08-26T23:09:00.563785Z"
    },
    {
        "instrument": "iEUR",
        "quote": 2.19572761275879,
        "timestamp": "2019-08-26T23:09:00.565883Z"
    }
Kogelet
  • 395
  • 5
  • 14
  • What do you mean by duplicated? Do they have the same ids? – Selcuk Aug 26 '19 at 23:34
  • one thing to look into would be the `max_length=4` on your `id` when you're passing length 7 strings as the pk. I'm not sure exactly how postgres and/or django will handle that since it's fairly atypical to use CharFields as pks. Perhaps there is some odd truncation going on. – azundo Aug 27 '19 at 02:14
  • FYI the `blank` and `null` attributes are False by default, so adding `null=False` to a model field doesn't do anything. – Lord Elrond Aug 27 '19 at 03:27
  • 1
    Show us the duplicate / triplicate instances. And show us more context of the code, are you doing this in the shell or is this happening inside a view? In the latter case, I'd like to see the entire view. – dirkgroten Aug 27 '19 at 10:20
  • @Selcuk ids are unique. It looks like there the object is created and committed several times. – Kogelet Aug 27 '19 at 13:21
  • @azundo Yes, in the example above I had a longer string but I usually pass string that have exactly 4 characters. I don't think that this is related to duplicated records in db. – Kogelet Aug 27 '19 at 13:36
  • @CalebGoodman thanks, good to know. I will adjust that part of the code. – Kogelet Aug 27 '19 at 13:36
  • @dirkgroten I have updated my questions and added duplicated records with almost similar timestamp. – Kogelet Aug 27 '19 at 13:39
  • 1
    ok, but you should show us **all the code** around the two lines where you save the new `Quote`. You just mentioned Apscheduler, but that is crucial, because obviously your lines of code are called multiple times. Without knowing what function is calling these lines of code, there's no way to tell you what's wrong. It's definitely not possible that running that line of code once produces 3 objects. – dirkgroten Aug 27 '19 at 13:43
  • @Kogelet Nothing with your current code appears wrong, so we have no idea what is causing this. Can you add an update with the view that runs this query? – Lord Elrond Aug 27 '19 at 17:06
  • Also, I want to point out that you can simply add `_id` in this line: `q = Quote.objects.create(instrument_id='EUR', quote=1.2000)`, and avoid making that second query. – Lord Elrond Aug 27 '19 at 17:07

1 Answers1

0

It turned out that the duplicates where created due to Apscheduler task that was launched by 3 workers. Here's a similar issue.

I have switched to Celery instead of Aspscheduler.

Kogelet
  • 395
  • 5
  • 14