title: SQLAlchemy Integrity Error UNIQUE constraint failed: users.login
body: <p>I'm making a web dashboard using Flask and it has Discord login (Flask-Dance). The code gives me this error: <code>sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) UNIQUE constraint failed: users.login</code>. The code works completely fine if the user is already logged in. But when someone else who is not logged in tried to log in, the error occurs. This is what I am working with currently:</p>
  <pre class="lang-py prettyprint-override"><code>class User(UserMixin, dbsl.Model):
      id = dbsl.Column(dbsl.Integer, primary_key=True)
      email = dbsl.Column(dbsl.String(256), unique=True)
  
  
  class OAuth(OAuthConsumerMixin, dbsl.Model):
      user_id = dbsl.Column(dbsl.Integer, dbsl.ForeignKey(User.id), nullable=False)
      user = dbsl.relationship(User)
  
  @oauth_authorized.connect_via(d_oauth)
  def discord_logged_in(blueprint, token):
      resp = discord_oauth.discord.get(&quot;/api/users/@me&quot;).content
  
      info = json.loads(resp)
      user_id = info[&quot;id&quot;]
  
      query = OAuth.query.filter_by(provider=blueprint.name, user_id=user_id)
      try:
          oauth = query.one()
      except NoResultFound:
          oauth = OAuth(user_id=user_id, token=token)
  
      if oauth.user:
          login_user(oauth.user)
  
      else:
          user = User(email=info[&quot;email&quot;], id=user_id)
          oauth.user = user
          dbsl.session.add_all([user, oauth])
          dbsl.session.commit()
          login_user(user)
  
      return False
  
  </code></pre>
  <p>As I said, the program works completely fine, but it throws this error when someone who is not logged in tries to log in. How can I fix this?</p>