Re: transactions and unique variables:

2007-03-15 Thread Bram - Smartelectronix

Tim Chase wrote:
> Which follows the pattern
> 
>UPDATE tbl
>SET field = (
>  SELECT SUM(field)
>  FROM tbl
>  WHERE [EMAIL PROTECTED] or [EMAIL PROTECTED]
>  ) - field
>WHERE [EMAIL PROTECTED] or [EMAIL PROTECTED]
> 
> Any help?
> 
> -tkc (aka "the atomic swapper"? :)

Hehehe, that's a pretty neat trick... Could you write that in django ORM 
for me? ;-)))

Seeing I only need to do this very infrequently, I think I'll just do 
the 3 saves...

tmp = items.__class__.order_by("-field")[0].field + 1
tmp, i1.field = i1.field, tmp
i1.save()
i1.field, i2.field = i2.field, tmp
i2.save()
i1.save()

ugly, but gets the job done...


  - bram

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: transactions and unique variables:

2007-03-15 Thread Tim Chase

> Uhm, please ignore this email, it looks like this can't be done using 
> transactions
> 
> begin transaction;
> update book set "order"=2 where id=1;
> update book set "order"=1 where id=2;
> commit;

for atomic swapping, however, it can be done in a single obscure 
SQL statement:

   update book set order = 3 - order
   where id=1 or id=2

Which follows the pattern

   UPDATE tbl
   SET field = (
 SELECT SUM(field)
 FROM tbl
 WHERE [EMAIL PROTECTED] or [EMAIL PROTECTED]
 ) - field
   WHERE [EMAIL PROTECTED] or [EMAIL PROTECTED]

Any help?

-tkc (aka "the atomic swapper"? :)



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: transactions and unique variables:

2007-03-15 Thread Bram - Smartelectronix

Bram - Smartelectronix wrote:
> Rubic wrote:
>> Bram,
>>
>> Try removing (commenting out) the transaction decorator
>> and transaction.commit(), then re-run your code.  The
>> ProgrammingError exception may be hiding the real
>> exception.  At least that's been my experience.
> 
> Nope..., I'm really getting the "unique key violation" on order...

Uhm, please ignore this email, it looks like this can't be done using 
transactions

begin transaction;
update book set "order"=2 where id=1;
update book set "order"=1 where id=2;
commit;

ERROR: duplicate key violates unique constraint "book_order_key"

What a pitty!


  - bram

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: transactions and unique variables:

2007-03-15 Thread Bram - Smartelectronix

Rubic wrote:
> Bram,
> 
> Try removing (commenting out) the transaction decorator
> and transaction.commit(), then re-run your code.  The
> ProgrammingError exception may be hiding the real
> exception.  At least that's been my experience.

Nope..., I'm really getting the "unique key violation" on order...


  - bram

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: transactions and unique variables:

2007-03-15 Thread Rubic

Bram,

Try removing (commenting out) the transaction decorator
and transaction.commit(), then re-run your code.  The
ProgrammingError exception may be hiding the real
exception.  At least that's been my experience.

--
Jeff Bauer
Rubicon, Inc.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---