Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

An error occurred when create_app was executed twice #75

Closed
frapples opened this issue Jul 22, 2017 · 4 comments
Closed

An error occurred when create_app was executed twice #75

frapples opened this issue Jul 22, 2017 · 4 comments

Comments

@frapples
Copy link

frapples commented Jul 22, 2017

This is create_app function:

import flask
from flask.ext.session import Session
from .models import db

sess = Session()


def create_app():
    app = flask.Flask(__name__)

    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
    app.config['SQLALCHEMY_ECHO'] = True

    app.config['SESSION_SQLALCHEMY'] = db
    app.config['SESSION_TYPE'] = 'sqlalchemy'
    sess.init_app(app)
    db.init_app(app)
    with app.app_context():
        db.create_all()

    return app

Then exec this code:

    app1 = create_app()
    app2 = create_app()

An error occurred:

Traceback (most recent call last):
  File "./runserver.py", line 12, in <module>
    main()
  File "./runserver.py", line 8, in main
    app2 = create_app()
  File "/home/computer/code/project/flownotebook/backend/flownotebook/__init__.py", line 18, in create_app
    sess.init_app(app)
  File "/usr/local/lib/python3.5/dist-packages/flask_session/__init__.py", line 61, in init_app
    app.session_interface = self._get_interface(app)
  File "/usr/local/lib/python3.5/dist-packages/flask_session/__init__.py", line 105, in _get_interface
    config['SESSION_PERMANENT'])
  File "/usr/local/lib/python3.5/dist-packages/flask_session/sessions.py", line 478, in __init__
    class Session(self.db.Model):
  File "/usr/local/lib/python3.5/dist-packages/flask_sqlalchemy/__init__.py", line 602, in __init__
    DeclarativeMeta.__init__(self, name, bases, d)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/ext/declarative/api.py", line 64, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/ext/declarative/base.py", line 131, in __init__
    self._setup_table()
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/ext/declarative/base.py", line 395, in _setup_table
    **table_kw)
  File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/schema.py", line 421, in __new__
    "existing Table object." % key)
sqlalchemy.exc.InvalidRequestError: Table 'sessions' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

If I comment sess.init_app(app), this code works fine.

I guess that the two app instance shared the same database, repeat exec the function create_all(), my own definition of those tables can be created good, but the sessions table by using flask-session can not work in this situation.

@scythargon
Copy link

same here. damned flask and his not supported batteries

@AllanChain
Copy link

AllanChain commented Sep 20, 2020

Using app factory for testing. This problem is still there and driving me crazy. Please fix it! (There is already a PR commit 1c1f790)

If modifying db is not graceful:

        for c in self.db.Model._decl_class_registry.values():
            if hasattr(c, '__table__') and c.__table__.fullname == table:
                self.sql_session_model = c
                return

Before defining class Session works for me

@ikoide
Copy link

ikoide commented Sep 27, 2021

I'd like to update @AllanChain's response which worked wonders for me. It appears since sqlalchemy 1.4 (sqlalchemy/sqlalchemy#6080) _decl_class_registry has become depreciated and replaced with _class_registry.

@Lxstr
Copy link
Contributor

Lxstr commented Mar 11, 2024

This issuse is now fixed from 0.7.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants