Just ran into a South migration issue. Basically we had a field that looked like this (simplified):
class MyModel( models.Model ):
hostname = models.CharField(
default = random_unique_hostname,
where random_unique_hostname() was written like so (again, simplified):
test = generate_random()
MyModel.objects.get( hostname = test )
except MyModel.DoesNotExist, err:
the actual code is significantly more complex, but that illustrates the problem. When you try to generate a South migration to add some *other* field to this model (i.e. a few months after you've added this default), you'll see the migration creation fail because South is calling your default (ouch) with a database that doesn't have the new field (the pre-migration DB).
The solution is to use a minimal query, rather than calling .get, use .filter().count() or use .filter().values_list('id') or any other query that doesn't map to the whole object being loaded, just to check for unique-ness.
Pingbacks are closed.