0

It is my first time encountered this error, I've implemented it multiple times on my different projects and migrated to database it works fine , but just this one returns me an error, when I tried to migrate my models.py it says:

django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint')

Now I have UserDetails class which linked to the AuthUser of Django migration I tried it multiple times in previous projects but this one has an error, I don't know the real issue here. it is my version of Python? mysql?

class AuthUser(models.Model):         //comes from the command : python manage.py inspectdb > models.py
    password = models.CharField(max_length=128)
    last_login = models.DateTimeField(blank=True, null=True)
    is_superuser = models.IntegerField()
    username = models.CharField(unique=True, max_length=150)
    first_name = models.CharField(max_length=150)
    last_name = models.CharField(max_length=150)
    email = models.CharField(max_length=254)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    date_joined = models.DateTimeField()

    class Meta:
        managed = False
        db_table = 'auth_user'

class UserDetails(models.Model):
    user_id = models.OneToOneField(AuthUser, on_delete=models.CASCADE)
    middle_name = models.CharField(max_length=128, blank=True, null=True)
    birthdate = models.DateField(blank=True, null=True)
    sex = models.CharField(max_length=128, blank=True, null=True)
    address = models.CharField(max_length=128, blank=True, null=True)
    position = models.CharField(max_length=128, blank=True, null=True)
    updated_at = models.DateTimeField(blank=True, null=True)

    class Meta:
        managed = True
        db_table = 'user_details'
2
  • Does this help: stackoverflow.com/questions/45960679/…? Commented Apr 20, 2023 at 8:09
  • @ruddra I already seen that post but I can't understand the whole solution at all Commented Apr 20, 2023 at 8:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.