This should work correctly if you're using a proper standard-compliant SQL database. The query would expand to
UPDATE car SET x = y, y = x WHERE <filter>
It would work correctly at least on PostgreSQL (also below), SQLite3 (below), Oracle and MSSQL, however the MySQL implementation is broken.
PostgreSQL:
select * from car;
x | y
---------+------
prefect | ford
(1 row)
test=> update car set x = y, y = x;
UPDATE 1
test=> select * from car;
x | y
------+---------
ford | prefect
(1 row)
SQLite3
sqlite> select * from foo;
prefect|ford
sqlite> update foo set x = y, y = x;
sqlite> select * from foo;
ford|prefect
However, MySQL violates the SQL standard,
mysql> insert into car values ('prefect', 'ford');
Query OK, 1 row affected (0.01 sec)
mysql> select * from car;
+---------+------+
| x | y |
+---------+------+
| prefect | ford |
+---------+------+
1 row in set (0.00 sec)
mysql> update car set x = y, y = x;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from car;
+------+------+
| x | y |
+------+------+
| ford | ford |
+------+------+
1 row in set (0.00 sec)
Thus there is a portable way to do this using a standard-compliant SQL database, but MySQL isn't one of them. If you cannot use the for
... save
loop, then you must resort to some of the hacks from Swapping column vales in MySQL; the temporary variable seems to be the most generic one; though I am not sure whether you can use the temporary variables with F
constructs of Django.