0

I am using graphene-django library and I have a User model that is inherited from AbstractUser. The username attribute is unique and it's ok for me. But The problem is that when I try to add a user with duplicated username id of user table in database increases. What's your idea?

It's my UserModel :

class User(AbstractUser):
    email = models.EmailField(unique=True)
    ...

and its my mutate function:

    def mutate(self, info, user_name, email, password, is_creator):
        if validateEmail(email=email):
            new_user = User.objects.create(username=user_name, email=email, password=password,
                                           is_creator=is_creator, created_at=datetime.datetime.now(),
                                           updated_at=datetime.datetime.now())
            new_user.save()
            ok = True
            logger.info(LogMessages.new_user_added.format(user_name, email))
            return UserCreator(user=new_user, ok=ok)
        else:
            error_message = LogMessages.input_email_is_invalid
            ok = False
            logger.info(LogMessages.input_email_is_invalid)
            return UserCreator(ok=ok, error_message=error_message)
  • Did you put a unique constraint on the username? – roganjosh Jul 07 '19 at 18:09
  • Yes, I did. I use the username attribute of AbstractUser and I don't customize it. – Muhammad Azhdari Jul 07 '19 at 18:13
  • 1
    You really should have a [mcve] to help here because that's the only decent guess I have, the rest will be stabs in the dark. We can't be expected to just guess. – roganjosh Jul 07 '19 at 18:14
  • ok, I will send my code examples. – Muhammad Azhdari Jul 07 '19 at 18:18
  • I updated the question. – Muhammad Azhdari Jul 07 '19 at 18:21
  • You have a unique constraint on the _email_, not the username – roganjosh Jul 07 '19 at 18:22
  • I told that I used the username attribute of AbstractUser. I don't override it. – Muhammad Azhdari Jul 07 '19 at 18:32
  • Don't expect `id`s to be anything other than unique: ["Because smallserial, serial and bigserial are implemented using sequences, there may be "holes" or gaps in the sequence of values which appears in the column, even if no rows are ever deleted. A value allocated from the sequence is still "used up" even if a row containing that value is never successfully inserted into the table column. This may happen, for example, if the inserting transaction rolls back."](https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL) – mu is too short Jul 07 '19 at 18:39
  • Closing this as a duplicate, since this is really just a postgres limitation as @muistooshort pointed out and there's already a number of questions relating to that – Daniel Rearden Jul 07 '19 at 19:31

0 Answers0