4

I've had a read through http://www.b-list.org/weblog/2006/jun/06/django-tips-extending-user-model/ but still am having problems migrating my new db schema with a ForeignKey for a user in a list of products

a simplified version of my code is:

from django.contrib.auth.models import User
# other imports

class Product(models.Model):

 author = models.ForeignKey(User)
 # other product fields..

but when I try to migrate to South, and this may be the problem, as my user database is not handled by South (??forgive my newbieness) i'm getting errors that a default value needs to be set:

_mysql_exceptions.OperationalError: (1067, "Invalid default value for 'author_id'")

Can anyone explain what i'm doing wrong?? Ideally I wouldn't want a default value for the user, or at least null, false or 0 (but that doesn't seem to work)..

many thanks,

adam

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
null
  • 1,137
  • 2
  • 11
  • 29

2 Answers2

0

If you want it to be possible to have an empty value for author, you need to define the ForeignKey with null=True. You'll probably want blank=True as well, as that controls the validation in Django forms.

author = models.ForeignKey(User, null=True, blank=True)
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • Ah thanks for you're help, still getting the same error though? running "python manage.py schemamigration search --auto" gives me 3 options: 1)quit now an add a default to the field (null=True, blank=True??) 2) specify a one-off value for existing columns (there aren't any) 3) disable the backwards migration by raising an exception (??!) – null Jan 24 '11 at 13:30
  • ..interestingly the form view does recognize the foreign relation, so the migration to south seems to be the problem.. highly confused! – null Jan 24 '11 at 13:38
0

Ended up taking the easy route and totally resetting the product table, and migrating from scratch which worked. Not sure i've learnt anything (other than how to reset an app)!

null
  • 1,137
  • 2
  • 11
  • 29