The problem is you are creating a new User record, which maps to an INSERT 
statment every request/log in.

Instead you should query first to find the record. Something like this:

        user = session.query(models.User).filter(
            models.User.username == username).first()
        if not user:
            user = models.User(...)
            session.merge(user)
        flask_login.login_user(user)

> On 6 Mar 2019, at 22:01, Sudhir Babu Pothineni <sbpothin...@gmail.com> wrote:
> 
> Hi Devs,
> 
> We have our own users database, I am doign liek this to authenticate:
> try:
>    cred = authenticate(session, username, password)
>    if cred:
>        user = models.User(
>            username=username,
>            is_superuser=False)
>        user.is_active=True
> 
>        session.merge(user)
>        flask_login.login_user(user)
>        session.commit()
>        return redirect(request.args.get("next") or url_for("admin.index"))
> except AuthenticationError:
>    flash("Incorrect login details")
>    return self.render('airflow/login.html',
>                       title="Airflow - Login",
>                       form=form)
> but its failing if user already exists, is there any alternative to check 
> existing user and keep forwarding?
> 
> IntegrityError: (sqlite3.IntegrityError) column username is not unique [SQL: 
> u'INSERT INTO users (username, email, superuser) VALUES (?, ?, ?)'] 
> [parameters: (u'testuser', None, 0)] (Background on this error at: 
> http://sqlalche.me/e/gkpj)
> 
> 

Reply via email to