Home > Enterprise >  FOREIGN KEY constraint failed Django Models
FOREIGN KEY constraint failed Django Models

Time:10-25

I'm creating a small-ish django application using AllAuth for the authetncation, which I have customised myself to include some additional fields.

Part of the sites functionality requires me to refrence the logged in user through a Foreign Key multiple times and I'm approaching this through the custom model I created, called UserProfile; (I tried Django's User model & this also gave me errors)

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile')
    phone_number = models.CharField(max_length=30, null=False)
    postcode = models.CharField(max_length=500, null=True, blank=True)
    town_or_city = models.CharField(max_length=40, null=True, blank=True)
    street_address1 = models.CharField(max_length=80, null=True, blank=True)
    street_address2 = models.CharField(max_length=80, null=True, blank=True)
    county = models.CharField(max_length=80, null=True)

I'm referencing the above model in the activity table:

class Activity(models.Model):
    class Meta:
        verbose_name_plural = 'Activities'

    activity_id = models.CharField(max_length=32, null=False, editable=False)
    host = models.ForeignKey(UserProfile, on_delete=models.CASCADE)
    name = models.CharField(max_length=254, null=False, blank=False)
    date = models.DateField()
    start_time =models.TimeField()
    end_time = models.TimeField()
    duration = models.DurationField(blank=True, null=True)
    location = models.CharField(max_length=40, null=False, blank=False)
    description = models.CharField(max_length=140, null=False, blank=False)
    available = models.BooleanField(default=True)

Everything works smoothly, as I can use this to create one activity per user, however I want to make this a Many to One field, which required me to update from settings.AUTH_USER_MODEL to the Foreign Key.

Unfortunately, I'm getting the following error:

FOREIGN KEY constraint failed

when I try to add a new activity now, and I'm at a loss as to why this is happening.

If someone could point me in the right direction on how to create this many to one functionality, it would be great.

CodePudding user response:

I can't see your whole code so the answear may be wrong but it Looks like you dont have Primary Key in UserProfile

ForeignKey As Default takes Primary Key as argument

Potential solve:

class Activity(models.Model):    
    host = models.ForeignKey(UserProfile, on_delete=models.CASCADE)

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='profile', primary_key=True)

If it dosent solve your problem please put full error message

DOCS

CodePudding user response:

I would guess, from what information that you've provided, is that what has happened is that you've potentially got a user who doesn't have a user profile, so when you try to create the activity it fails with the error you specified. On the other hand, it could also be related to how you're rendering and validating your forms. Furthermore it could also be due to the way you are using class based views views or function based views.

In any case, please provide all information possible. Generally all information is required, not just the part you're unsure of.

How do you create an activity?

How do you validate your forms?

How is the user profile ID populated?

Providing more information, I will happily respond as it seems like a straight forward question

  • Related