Django add many to many with Big Integer as primay key

Viewed 142

I have users model with Positive Big integer field as the primary key, and I want to create a many to many relationship with Roles table, the issue is that the pivot table (containing the relationship) is being created with an integer field (user_id), which doesn't allow me to actually store any relationship for users with high ID number.

id = models.PositiveBigIntegerField(unique=True, primary_key=True)

This is my user model:

class User(AbstractBaseUser):
    id = models.PositiveBigIntegerField(unique=True, primary_key=True)
    email = models.EmailField(_("email address"), unique=True)
    display_name = models.CharField(_("Display Name"), max_length=64, blank=True)
    is_active = models.BooleanField(_("active"), default=True)

    is_deleted = models.BooleanField(default=False)

    roles = models.ManyToManyField(Role, related_name="%(app_label)s_%(class)s_related")

and this is my Role model:

class Role(models.Model):
    name = models.CharField(max_length=64)
    is_deleted = models.BooleanField(default=False)

Check screenshot of DB schema below

enter image description here

P.S: I tried creating the relationship manually with:

roles = models.ManyToManyField(Role, related_name="%(app_label)s_%(class)s_related", through='UserRoles')

and creating the table:

class UserRoles(models.Model):
    role = models.ForeignKey(Role, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE, to_field='id')

But that also didn't work.

0 Answers
Related