views:

29

answers:

1

I'm migrating data from a legacy database that has many tables with primary keys defined like:

`id` decimal(26,0) 

The id column will contain values that look like:

20080313154354198004
20081217165552136057
2008080416222952067
20060510151423191000
20060510151423191000
20070710143455874025
200806239353171091

Doing queries like:

Release.objects.raw("SELECT id from games_release where legacy_id = 2008050812286746069")

will sometimes succeed and sometimes fail. In the case of failure I can capture the query from db.connection.queries, paste it into MySQL console, and the query will succeed. I imagine it has to with how the DECIMAL type is handled at the various levels of the stack (ORM -> DB driver -> server). This seemed like it was relevant but I haven't had any luck with CAST() so far:

http://dev.mysql.com/doc/refman/5.1/en/type-conversion.html

Any help on making the queries work reliably much appreciated.

Edit One piece to the puzzle I forgot to mention: The decimal values I'm attempting to query on are stored in a table that were originally pulled from the legacy table. For example:

class OldModel(db.models):
    id = models.DecimalField(decimal_places=0, max_digits=36)

class NewModel(db.models):
    id = models.DecimalField(decimal_places=0, max_digits=36)

old = <some instance of OldModel>
new = NewModel.objects.create(id=old.id)

Then sometime later, trying to select for "new" via objects.raw() may fail.

+1  A: 

Found a solution - cast the Decimal parameter to an integer and use %s as the param placeholder:

Release.objects.raw("SELECT id from games_release where legacy_id = %s", [int(uc.game_release_id)])
Brian Luft
@Brian Luft: You should accept your own answer so that others know a solution has been found.
Mike
Done, thanks. StackOverflow imposes a wait time on accepting your own answers or I'd have done it sooner :)
Brian Luft