views:

364

answers:

3

I have this model

class Vacancy(models.Model):
    user = models.ForeignKey(User, null=True, blank=True, default = None)
    name = models.CharField(max_length=64)

When in admin i try to creat a vacancy without a user. And it throws an error " club_vacancy.user_id may not be NULL". Am i doing something wrong?

+3  A: 

club_vacancy.user_id may not be NULL

Looks very much like an error from your database, rather than from Django.

It seems most likely that you added null=True after running manage.py syncdb. You'll need to modify your database schema to allow null values in that column.

Ben James
well i ran syncdb before and after i added null=true. How should i modify my db?
barin
syncdb does not modify tables which already exist. Drop the table and run syncdb again, or modify your database manually, or use a migrations app.
Ben James
migrations app like 'south'
panchicore
i didn't know how to drop a table so i deleted the db and creted it again. It works now.
barin
A: 

you need to reset you database table (since just syncdb does not update fields that are alredy created with null=False)

./manage.py reset your_app

OR if there is some data that you do not want to loose use SQL commands to remove NOT NULL flag

Pydev UA
A: 

Aside from South, another option is to use django evolution for schema changes.

http://code.google.com/p/django-evolution/

Install it before making db changes. Then run

python manage.py evolve --hint --execute

Make sure that if you add a new field, you allow nulls (null=True) or else evolve will give you an error message.

dannyroa
what if i don't want to allow the new field to be null? What do i do?
barin