IntegrityError Django Model

I'm trying to implement a foreign key at one of my tables, but I keep getting this error all the time:

null value in column "user_id" of relation "application_application" violates not-null constraintDETAIL: Failing row contains (3, 2022-09-09, San Ramon, CA, USA, https://logo.clearbit.com/uber.com, Uber, , Applied, Engineer, null).

I don't know what I'm doing wrong

from django.contrib.auth.models import User

class Application(models.Model):

    __tablename__ = "applications"

    name = models.CharField(max_length=50, blank=False, null=False)
    title = models.CharField(max_length=50, blank=False, null=False)
    status = models.CharField(max_length=50, blank=False, null=False)
    notes = models.TextField(blank=True, default="Add some notes", null=True)
    location = models.CharField(max_length=255, blank=False, null=False)
    logo = models.CharField(max_length=255, blank=False, null=False)
    date_applied = models.CharField(max_length=255, blank=False, null=False)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    
    def __str__(self):
        return self.name

It looks like you're not passing in the user when you're trying to save. Try this:

app = Application(user=request.user, name='App X', etc)
app.save()
Back to Top