pallets-eco / flask-session

Server side session extension for Flask
https://flask-session.readthedocs.io
BSD 3-Clause "New" or "Revised" License
507 stars 238 forks source link

Error from version 0.6.0 to 0.8.0 on loading #247

Closed jgriffon closed 5 months ago

jgriffon commented 6 months ago

Hello,

I've updated from 0.5.0 to 0.8.0 and when I load my project I have this error (see below) I' ve tried mutliple things and problem appears with 0.6.0. Before it was ok and nothing else is changed. Any idea of what I have to do ?

An extract of my settings :

    SQLALCHEMY_DATABASE_URI = "mssql+pyodbc://%(user)s:%(pw)s@%(dsn)s" % hshSetting["SQL_SERVER"]
    SESSION_TYPE = "sqlalchemy"
    SESSION_PERMANENT = True
    SESSION_SQLALCHEMY_TABLE = "web_sessions"
    SESSION_SQLALCHEMY_SCHEMA = "dbo"
    PERMANENT_SESSION_LIFETIME = 43200  # 12h

and my partial init.py :

"""init"""

# pylint: disable=import-outside-toplevel
import logging
import os

import urllib3
from elasticapm.contrib.flask import ElasticAPM
from flask import Flask, request
from flask_ldap3_login import LDAP3LoginManager
from flask_login import LoginManager, current_user
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.ext.declarative import as_declarative
from sqlalchemy.orm import DeclarativeBase

from .config import CONFBASE

urllib3.disable_warnings()
apm = ElasticAPM()
ss_session = Session()
# pylint: disable-next=invalid-name
log = None

@as_declarative()
class Base(DeclarativeBase):
    """Base"""

    def to_dict(self):
        """Return object as a dict"""
        return {c.key: getattr(self, c.key) for c in self.__mapper__.column_attrs}

db = SQLAlchemy(model_class=Base)

# # Configure authentication
login_manager = LoginManager()
login_manager.session_protection = "strong"
login_manager.login_view = "auth.login"
login_manager.login_message = "Merci de vous authentifier pour accéder à cette page."

ldap_manager = LDAP3LoginManager()  # Setup a LDAP3 Login Manager.

# pylint:disable-next=unused-argument
def create_app(config_name):
    """create_app"""
    # pylint: disable-next=global-statement
    global log
    app = Flask(__name__)
    app.config.from_object(CONFBASE)
    app.secret_key = os.urandom(12)

    gunicorn_logger = logging.getLogger("gunicorn.error")
    app.logger.handlers = gunicorn_logger.handlers
    app.logger.setLevel(gunicorn_logger.level)
    log = app.logger
    log.info("Démarrage : InterSyMec")

    apm.init_app(
        app,
        server_url="https://docker-prod:8200",
        service_name=app.name,
        logging=logging.ERROR,
        log_level="error",
        debug=True,
        # Gestion certificat
        verify_server_cert=False,
        # SERVER_CERT="path\to\ca.crt",
        # Limitation des données
        DISABLE_METRICS="*",
    )
    db.init_app(app)
    login_manager.init_app(app)
    ldap_manager.init_app(app)
    app.config["SESSION_SQLALCHEMY"] = db
    ss_session.init_app(app)

(pyodbc.Error) ('HY104', '[HY104] [Microsoft][ODBC SQL Server Driver]Valeur de précision non valide (0) (SQLBindParameter)')
[SQL: SELECT [INFORMATION_SCHEMA].[TABLES].[TABLE_NAME] 
FROM [INFORMATION_SCHEMA].[TABLES] 
WHERE ([INFORMATION_SCHEMA].[TABLES].[TABLE_TYPE] = CAST(? AS NVARCHAR(max)) OR [INFORMATION_SCHEMA].[TABLES].[TABLE_TYPE] = CAST(? AS NVARCHAR(max))) AND [INFORMATION_SCHEMA].[TABLES].[TABLE_NAME] = CAST(? AS NVARCHAR(max)) AND [INFORMATION_SCHEMA].[TABLES].[TABLE_SCHEMA] = CAST(? AS NVARCHAR(max))]
[parameters: ('BASE TABLE', 'VIEW', 'web_sessions', 'dbo')]
(Background on this error at: https://sqlalche.me/e/20/dbapi)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1967, in _exec_single_context
    self.dialect.do_execute(
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\default.py", line 924, in do_execute
    cursor.execute(statement, parameters)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\elasticapm\instrumentation\packages\dbapi2.py", line 234, in execute
    return self._trace_sql(self.__wrapped__.execute, sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\elasticapm\instrumentation\packages\dbapi2.py", line 273, in _trace_sql
    result = method(sql, params)
             ^^^^^^^^^^^^^^^^^^^
pyodbc.Error: ('HY104', '[HY104] [Microsoft][ODBC SQL Server Driver]Valeur de précision non valide (0) (SQLBindParameter)')

During handling of the above exception, another exception occurred:

  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\elasticapm\instrumentation\packages\dbapi2.py", line 273, in _trace_sql
    result = method(sql, params)
             ^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\elasticapm\instrumentation\packages\dbapi2.py", line 234, in execute
    return self._trace_sql(self.__wrapped__.execute, sql, params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\default.py", line 924, in do_execute
    cursor.execute(statement, parameters)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1986, in _exec_single_context
    self._handle_dbapi_exception(
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 2353, in _handle_dbapi_exception
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1986, in _exec_single_context
    self._handle_dbapi_exception(
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1846, in _execute_context
    return self._exec_single_context(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1640, in _execute_clauseelement
    ret = self._execute_context(
          ^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\sql\elements.py", line 515, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 1418, in execute
    return meth(
           ^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 3367, in _internal_has_table
    c = connection.execute(s)
        ^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\reflection.py", line 88, in cache
    return fn(self, con, *args, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<string>", line 2, in _internal_has_table
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 3264, in has_table
    return self._internal_has_table(connection, tablename, owner, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 2875, in _switch_db
    return fn(*arg, **kw)
           ^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\dialects\mssql\base.py", line 2851, in wrap
    return _switch_db(
           ^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\sql\ddl.py", line 862, in _can_create_table
    return not self.checkfirst or not self.dialect.has_table(
                                      ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\sql\ddl.py", line 935, in visit_table
    if not create_ok and not self._can_create_table(table):
                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\sql\visitors.py", line 664, in traverse_single
    return meth(obj, **kw)
           ^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 2457, in _run_ddl_visitor
    visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\engine\base.py", line 3251, in _run_ddl_visitor
    conn._run_ddl_visitor(visitorcallable, element, **kwargs)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\sqlalchemy\sql\schema.py", line 1288, in create
    bind._run_ddl_visitor(ddl.SchemaGenerator, self, checkfirst=checkfirst)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask_session\sqlalchemy\sqlalchemy.py", line 112, in __init__
    self.sql_session_model.__table__.create(bind=engine, checkfirst=True)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask_session\__init__.py", line 165, in _get_interface
    session_interface = SqlAlchemySessionInterface(
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask_session\__init__.py", line 41, in init_app
    app.session_interface = self._get_interface(app)
                            ^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\intersymec\__init__.py", line 79, in create_app
    ss_session.init_app(app)
  File "D:\JG\Git\Web\interSyMec\manage.py", line 6, in <module>
    app = create_app(os.getenv("intersymec_ENV") or "dev")
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask\cli.py", line 245, in locate_app
    __import__(module_name)
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask\cli.py", line 335, in load_app
    app = locate_app(import_name, name)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask\cli.py", line 953, in run_command
    raise e from None
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask\cli.py", line 953, in run_command
    raise e from None
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\click\decorators.py", line 92, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\click\core.py", line 783, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\click\core.py", line 1434, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\click\core.py", line 1688, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\click\core.py", line 1078, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask\cli.py", line 1105, in main
    cli.main()
  File "D:\JG\Git\Web\interSyMec\.venv\Lib\site-packages\flask\__main__.py", line 3, in <module>
    main()
  File "C:\Python\Python311\Lib\runpy.py", line 88, in _run_code
    exec(code, run_globals)
  File "C:\Python\Python311\Lib\runpy.py", line 198, in _run_module_as_main (Current frame)
    return _run_code(code, main_globals, None,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
sqlalchemy.exc.DBAPIError: (pyodbc.Error) ('HY104', '[HY104] [Microsoft][ODBC SQL Server Driver]Valeur de précision non valide (0) (SQLBindParameter)')
[SQL: SELECT [INFORMATION_SCHEMA].[TABLES].[TABLE_NAME] 
FROM [INFORMATION_SCHEMA].[TABLES] 
WHERE ([INFORMATION_SCHEMA].[TABLES].[TABLE_TYPE] = CAST(? AS NVARCHAR(max)) OR [INFORMATION_SCHEMA].[TABLES].[TABLE_TYPE] = CAST(? AS NVARCHAR(max))) AND [INFORMATION_SCHEMA].[TABLES].[TABLE_NAME] = CAST(? AS NVARCHAR(max)) AND [INFORMATION_SCHEMA].[TABLES].[TABLE_SCHEMA] = CAST(? AS NVARCHAR(max))]
[parameters: ('BASE TABLE', 'VIEW', 'web_sessions', 'dbo')]
(Background on this error at: https://sqlalche.me/e/20/dbapi)```

Thanks for your help
jgriffon commented 5 months ago

Hi,

I've completed other actions and understood the problem !

My ODBC driver was SQLServer 10 (I'm on Windows). I've changed to new ODBC 18 Sql server and now all is working well