An Error When i use UserManager

Hello/Bonsoir à vous

I have an error when I try to use UserManager in my flask application.
Here’s an overview of the error.

  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/orm/query.py", line 1136, in _get_impl
    return self.session._get_impl(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 3873, in _get_impl
    return db_load_fn(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/orm/loading.py", line 694, in load_on_pk_identity
    session.execute(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2362, in execute
    return self._execute_internal(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 2247, in _execute_internal
    result: Result[Any] = compile_state_cls.orm_execute_statement(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/orm/context.py", line 305, in orm_execute_statement
    result = conn.execute(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1418, in execute
    return meth(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 515, in _execute_on_connection
    return connection._execute_clauseelement(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1640, in _execute_clauseelement
    ret = self._execute_context(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1846, in _execute_context
    return self._exec_single_context(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1986, in _exec_single_context
    self._handle_dbapi_exception(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2355, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
    self.dialect.do_execute(
  File "/home/gnuhealth/Flask/flaskvenv/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 941, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.DataError: (psycopg2.errors.InvalidTextRepresentation) invalid input syntax for type integer: "None"
LINE 3: WHERE users.id = 'None'
                         ^

[SQL: SELECT users.id AS users_id, users.is_active AS users_is_active, users.email AS users_email, users.email_confirmed_at AS users_email_confirmed_at, users.password AS users_password, users.is_admin AS users_is_admin, users.is_confirmed AS users_is_confirmed, users.confirmed_on AS users_confirmed_on, users.first_name AS users_first_name, users.last_name AS users_last_name 
FROM users 
WHERE users.id = %(pk_1)s]
[parameters: {'pk_1': 'None'}]
(Background on this error at: https://sqlalche.me/e/20/9h9h)

Here is my app

app.config.from_object(__name__+'.ConfigClass')
app.config['TRYTON_CONFIG'] = '/home/gnuhealth/gnuhealth/tryton/server/config/trytond.conf'
app.config['TRYTON_DATABASE'] = 'PDMD_SANTE'
app.config['TRYTON_USER'] = 0
app.config['CORS_HEADERS'] = 'Content-Type'

CORS(app, resources={r"*": {"origins": "*"}})

tryton = Tryton(app)
db = SQLAlchemy(app)
#db.init_app(app)
mail = Mail(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "auth.login"
login_manager.logout_view = "auth.logout"

with app.app_context():
    db.create_all()

from .accounts.models import User
user_manager=UserManager(app, db, User)

@login_manager.user_loader
def load_user(user_id):
    return User.query.filter(User.id == int(user_id)).first()

#@login_manager.user_loader
#def load_user(user):
#    return User.query.get(int(user))

from .accounts.users import userd
from .accounts.auth import auth
from .doctor import doctor
app.register_blueprint(doctor)
app.register_blueprint(userd)
app.register_blueprint(auth)

print("---------------------------", current_user)
    # a simple page that says hello
@app.route('/hello')
def hello():
    return 'Hello, World!'

Does anyone have any idea what I can do?
Thank You

You’re using both SQLAlchemy and Tryton to access the data.
It looks like a recipe for disaster :slight_smile:. I would stick to one or the other.

That being said where does UserManager come from? Is it from Flask-Login? If so I guess you have to interface the different methods used by the flask-login API to retrieve a tryton user and to authenticate it.

I do not know what is UserManager and I do not understand why you are using SQLAlchemy if you are using flask-tryton.
Any way it seems that something is searching for the user with the id 'None'. For me it is unrelated to flask-tryton nor tryton.

My guess would be that you should check user_id before using it in: