diff --git a/src/flask_session/__init__.py b/src/flask_session/__init__.py index b95a0b05..71a09964 100644 --- a/src/flask_session/__init__.py +++ b/src/flask_session/__init__.py @@ -91,6 +91,9 @@ def _get_interface(self, app): SESSION_SQLALCHEMY_TABLE = config.get( "SESSION_SQLALCHEMY_TABLE", Defaults.SESSION_SQLALCHEMY_TABLE ) + SESSION_SQLALCHEMY_CREATE_TABLE = config.get( + "SESSION_SQLALCHEMY_CREATE_TABLE", Defaults.SESSION_SQLALCHEMY_CREATE_TABLE + ) SESSION_SQLALCHEMY_SEQUENCE = config.get( "SESSION_SQLALCHEMY_SEQUENCE", Defaults.SESSION_SQLALCHEMY_SEQUENCE ) @@ -166,6 +169,7 @@ def _get_interface(self, app): **common_params, client=SESSION_SQLALCHEMY, table=SESSION_SQLALCHEMY_TABLE, + create_table=SESSION_SQLALCHEMY_CREATE_TABLE, sequence=SESSION_SQLALCHEMY_SEQUENCE, schema=SESSION_SQLALCHEMY_SCHEMA, bind_key=SESSION_SQLALCHEMY_BIND_KEY, diff --git a/src/flask_session/defaults.py b/src/flask_session/defaults.py index 7f890d6e..5f619762 100644 --- a/src/flask_session/defaults.py +++ b/src/flask_session/defaults.py @@ -39,6 +39,7 @@ class Defaults: SESSION_SQLALCHEMY_SEQUENCE = None SESSION_SQLALCHEMY_SCHEMA = None SESSION_SQLALCHEMY_BIND_KEY = None + SESSION_SQLALCHEMY_CREATE_TABLE = True # DynamoDB settings SESSION_DYNAMODB = None diff --git a/src/flask_session/sqlalchemy/sqlalchemy.py b/src/flask_session/sqlalchemy/sqlalchemy.py index 4841c84b..8bfb4c67 100644 --- a/src/flask_session/sqlalchemy/sqlalchemy.py +++ b/src/flask_session/sqlalchemy/sqlalchemy.py @@ -86,6 +86,7 @@ def __init__( sequence: Optional[str] = Defaults.SESSION_SQLALCHEMY_SEQUENCE, schema: Optional[str] = Defaults.SESSION_SQLALCHEMY_SCHEMA, bind_key: Optional[str] = Defaults.SESSION_SQLALCHEMY_BIND_KEY, + create_table: bool = Defaults.SESSION_SQLALCHEMY_CREATE_TABLE, cleanup_n_requests: Optional[int] = Defaults.SESSION_CLEANUP_N_REQUESTS, ): self.app = app @@ -103,13 +104,15 @@ def __init__( self.sql_session_model = create_session_model( client, table, schema, bind_key, sequence ) - # Create the table if it does not exist - with app.app_context(): - if bind_key: - engine = self.client.get_engine(app, bind=bind_key) - else: - engine = self.client.engine - self.sql_session_model.__table__.create(bind=engine, checkfirst=True) + + # Optionally create the table if it does not exist + if create_table: + with app.app_context(): + if bind_key: + engine = self.client.get_engine(app, bind=bind_key) + else: + engine = self.client.engine + self.sql_session_model.__table__.create(bind=engine, checkfirst=True) super().__init__( app,