On Thu, Dec 6, 2012 at 6:18 AM, Francis Devereux <[email protected]> wrote:
> 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/'
Can't you just do the concatenation into a variable and that pass that in, e.g.:
newFile = 'travelbox/accommodation/' + F('url')
orm['travelbox.AccommodationImage'].objects.all().update(file=newFile)
--
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.