Hi,
I currently have the following code in one of my migrations:
cursor = connection.cursor()
cursor.execute('''UPDATE travelbox_accommodationimage SET file =
CONCAT('travelbox/accommodation/', url)''')
transaction.commit_unless_managed()
This works for us (on MySQL, which we are using due to hosting provider
constraints), but as a matter of interest I wonder whether it's possible to
avoid using raw SQL and the CONCAT function which may not be available in all
databases.
First I tried this:
orm['travelbox.AccommodationImage'].objects.all().update(file='travelbox/accommodation/'
+ F('url'))
However that doesn't work - it executes the following SQL:
UPDATE `travelbox_accommodationimage` SET `file` =
'travelbox/accommodation/' + `travelbox_accommodationimage`.`url`;
args=('travelbox/accommodation/',)
which tries to use '+' on strings which fails with this error:
_mysql_exceptions.Warning: Truncated incorrect DOUBLE value:
'travelbox/accommodation/'
Obviously I could iterate through all of the rows and update them individually,
but is there a way of doing bulk concatenations on string columns using the
Django ORM?
Thanks,
Francis
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
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.