#27791: update_or_create() doesn't work properly when model has auto_now=True
-------------------------------------+-------------------------------------
               Reporter:  Andrew     |          Owner:  nobody
  Chiw                               |
                   Type:  Bug        |         Status:  new
              Component:  Database   |        Version:  1.10
  layer (models, ORM)                |
               Severity:  Normal     |       Keywords:
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I'm writing an exchange rate app:

 models.py
 {{{
 from django.db import models


 class ExchangeRate(models.Model):
     base = models.CharField(max_length=3)
     target = models.CharField(max_length=3)
     rate = models.DecimalField(max_digits=17, decimal_places=8)
     date = models.DateField(auto_now=True)

     def __str__(self):
         return "{}->{} on {}".format(self.base, self.target,
 str(self.date))

     class Meta:
         unique_together = (("base", "target", "date"),)

 }}}

 fx.py
 {{{
 from datetime import datetime, date
 import requests

 from .models import ExchangeRate


 def get_eur_rates():
     url = 'http://api.fixer.io/latest'
     eur_rates = requests.get(url).json()
     print(eur_rates)
     for target in eur_rates["rates"]:
         import ipdb; ipdb.set_trace()
         ExchangeRate.objects.update_or_create(base="EUR", target=target,
 date=eur_rates["date"], defaults={"rate": eur_rates["rates"][target]})
     print(ExchangeRate.objects.all())
 }}}

 I already have ExchangeRate objects in the database from 2017-01-29.
 Now, when I want to run update_or_create() with date='2017-01-27', it
 fails:

 {{{
 ipdb> ExchangeRate.objects.update_or_create(base="EUR", target=target,
 date='2017-01-27', defaults={"rate": eur_rates["
 rates"][target]})
 *** django.db.utils.IntegrityError: duplicate key value violates unique
 constraint "fx_exchangerate_base_f6916782_uniq"
 DETAIL:  Key (base, target, date)=(EUR, CZK, 2017-01-29) already exists.
 }}}

 I'm sure Django is working internally in a way that might result in this
 error, but this shouldn't happen and the error message is confusing.

--
Ticket URL: <https://code.djangoproject.com/ticket/27791>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/057.df512fa10f790ab6ddc140d3d3a9c8e1%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to