Re: Locking / serializing access to one element in database

2015-11-05 Thread Carsten Fuchs
Hi Collin, Am 05.11.2015 um 16:36 schrieb Collin Anderson: If you're just updating one field, this _might_ work for you: Why just one? | try: TestMonthModel.objects.create(jahr=Jahr,monat=Monat)# create() uses force_insert. exceptIntegrityError: pass # It already exists. No Proble

Re: Locking / serializing access to one element in database

2015-11-05 Thread Collin Anderson
Hi Carsten, If you're just updating one field, this _might_ work for you: try: TestMonthModel.objects.create(jahr=Jahr, monat=Monat) # create() uses force_insert. except IntegrityError: pass # It already exists. No Problem. # ... calculate some things. TestMonthModel.objects.filter(jahr

Re: Locking / serializing access to one element in database

2015-10-28 Thread Carsten Fuchs
Hi Collin, hi all, Am 27.10.2015 um 19:56 schrieb Collin Anderson: Yes, an exception will be raised. Thinking further about this, all we need is a method that gives us an exception if we accidentally create a second object when in fact only one is wanted. Your suggestion with manually dealin

Re: Locking / serializing access to one element in database

2015-10-27 Thread Collin Anderson
Yes, an exception will be raised. As an example, Django uses force_insert when creating sessions: https://github.com/django/django/blob/32ef48aa562e6aaee9983f5d0f1c60f02fd555fb/django/contrib/sessions/backends/db.py#L86 On Saturday, October 24, 2015 at 2:36:15 PM UTC-4, Joakim Hove wrote: > > Hi

Re: Locking / serializing access to one element in database

2015-10-24 Thread Joakim Hove
Hi Collin; thank you for your suggestion: [...] if the thread thinks that the object is new, it could try using > .save(force_insert=True). > I have read to read the force_insert documentation without understanding 100%. Assume threads t1 and t2 are racing to create the first element with th

Re: Locking / serializing access to one element in database

2015-10-24 Thread Joakim Hove
> > > This only covers the case where the object with the given ID already > exists, doesn't it? > Yes - it must be so; thank you for pointing out. In my particular case I can get around it be pre creating an empty element, but that is not very nice - I will look at Collins suggestion. --

Re: Locking / serializing access to one element in database

2015-10-24 Thread Collin Anderson
Hi Carsten, Something that might help: depending on how your unique keys are set up, if the thread thinks that the object is new, it could try using .save(force_insert=True). That way it won't overwrite a different object with the same PK. Thanks, Collin On Thursday, October 22, 2015 at 9:56:

Re: Locking / serializing access to one element in database

2015-10-22 Thread Carsten Fuchs
Am 22.10.2015 um 01:16 schrieb Simon Charette: I would suggest you use select_for_update() in a transaction. This only covers the case where the object with the given ID already exists, doesn't it? That is, if a

Re: Locking / serializing access to one element in database

2015-10-22 Thread Joakim Hove
Thank you; > I would suggest you use select_for_update() in a transaction. That seems to be just what I want! -- You received this message because you are subscribed to the Google Groups "Django users" group. T

Re: Locking / serializing access to one element in database

2015-10-21 Thread Simon Charette
Hi Joakim, I would suggest you use select_for_update() in a transaction. It's hard to provide a full example without more details about the kind of calculation required. Does it need to be run on new_text even if n

Locking / serializing access to one element in database

2015-10-21 Thread Joakim Hove
Hello; this arises in the context of a django application - but it might be a more general Python/Postgres/... problem. [ The django application is existing all right - but the problem I am describing here is yet only in my head; I am seeking advice on how to proceed. ] Assume I have a model