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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With