Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OneToOneField with null=True doesn't allow empty field

I've got these two classes:

class Bill(models.Model):
  date = models.DateField()
  total_amount_chf = models.DecimalField('Cost (in CHF)', max_digits=10, decimal_places=2)

class ProjectParticipation(models.Model):
  project = models.ForeignKey('Project')
  user = models.ForeignKey(User)
  is_admin = models.BooleanField()
  bill = models.OneToOneField(Bill, on_delete=models.SET_NULL, null=True, blank=True)

When I'm now constructing the SQL-database I get the following field in the table for the ProjectParticipation:

 bill_id integer NOT NULL,
 CONSTRAINT expenses_projectparticipation_bill_id_fkey FOREIGN KEY (bill_id)
  REFERENCES expenses_bill (id) MATCH SIMPLE
  ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,

And now when I want to insert a ProjectParticipation without Bill I get a "null value in column "bill_id" violates not-null constraint".

What to do against it?

like image 856
melbic Avatar asked Dec 28 '12 11:12

melbic


1 Answers

May be you have added the Null Constraint later after syncing the database. Delete the database and re-sync the database (if you are not using Django-South otherwise make sure you have migrated the schema changes)

like image 61
Aamir Rind Avatar answered Oct 21 '22 21:10

Aamir Rind