0

I have django model:

#models.py

class m1(models.Model):
    some_word = models.CharField()

I've made db with 'manage.py syncdb' command and then added a ForeignKey field to a model

#models.py

from django.contrib.auth.models import User
class m1(models.Model):
    some_word = models.CharField()
    user = models.ForeignKey(User)

As far as I know, native django doesn't provide db modifying, so I need to create column 'user_id' manually. Ok, I go to phpmyadmin and create this column. At this moment everything is ok and app works. But then I understand that I need option 'blank = True' at the user field:

#models.py

from django.contrib.auth.models import User
class m1(models.Model):
    some_word = models.CharField()
    user = models.ForeignKey(User, blank = True)

And while saving any m1 object, I got error 'Cannot assign None: "m1.user" does not allow null values'. I know that if I delete table and make it with syncdb, everything will be fine, and if I use migrating tool like South, it will be ok too. But I want to understand mechanics of this and find out, what is wrong with db.

I call 'manage.py sqlall app' and it tells 'CREATE TABLE' and create indexes:

CREATE INDEX `app_m1_a703c35a` ON `app_m1` (`user_id`);

ALTER TABLE `app_m1` ADD CONSTRAINT `user_id_refs_id_5b1b34cc` 
FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);

Ok, I go to phpmyadmin and do this sql stuff (frankly, i don't know what these indexes do). But the error is still occurs. What should I do?

Павел Тявин
  • 2,529
  • 4
  • 25
  • 32

1 Answers1

0

Firstly, you should use South to do these database migrations for you, especially as you admit you don't really understand what's going on.

Secondly, the error is not because it's missing blank=True, but because it's missing null=True, which is a completely different thing. The documentation explains this well.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895