Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ForeignKey does not allow null values

I am using the Django REST Framework 2.0.

Here is my model class:

class Mission(models.Model):   assigned_to = models.ForeignKey('auth.User',                                    related_name='missions_assigned',                                    blank = True) 

Here is my view class:

class MissionList(generics.ListCreateAPIView):     model = Mission     serialize_class = MissionSerializer 
  1. The multipart form is rendered in the browser with empty choice for assigned_to field.

  2. When posting raw JSON, I get the following error message:

Cannot assign None: "Mission.assigned_to" does not allow null values.

like image 577
Benjamin Toueg Avatar asked May 16 '13 13:05

Benjamin Toueg


People also ask

Can a ForeignKey be null?

A foreign key containing null values cannot match the values of a parent key, since a parent key by definition can have no null values. However, a null foreign key value is always valid, regardless of the value of any of its non-null parts.

Does foreign key constraint allow null?

the foreign key, cannot be null by default in mySQL, the reason is simple, if you reference something and you let it null, you will loose data integrity. when you create the table set allow null to NOT and then apply the foreign key constraint.

Can ForeignKey be null in Django?

django rest framework - ForeignKey does not allow null values - Stack Overflow. Stack Overflow for Teams – Start collaborating and sharing organizational knowledge.

Can we insert null values in foreign key column?

Yes. If a column is marked as nullable while creating the table, you can insert null in column which is a foreign key. Yes,You can null value in Foreign key Column.


1 Answers

The blank option is used in the form validation, and the null is used when writing to database.

So you might add null=True to that field.

EDIT: continue the comment

Considering the two steps when saving object:

  1. Validator(controlled by blank)
  2. Database limitation(controlled by null)

For default option, take IntegerField for example,
default=5, blank=True, null=False, pass (1) even if you didn't assign a value(having blank=True), pass (2) because it has a default value(5) and writes 5 instead of None to DB.
blank=True, null=False, which pass (1) but not (2), because it attempts to write None to DB.

Thus, if you want to make a field optional, use either default=SOMETHING, blank=True, null=False or blank=True, null=True.

Another exception is the string-like field, such as CharField.
It's suggested that use the blank=True alone, leaving null=False behind.
This makes a field either a string(>=1 char(s)) or a empty string('', with len()==0), and never None.

The reason is that when null=True is set, there will be two possible value for the state "unset": empty string and None, which is confusing(and might causing bugs).

like image 144
pjw91 Avatar answered Oct 04 '22 05:10

pjw91