2

I have a User class:

class User(db.Model, UserMixin):
    """
    ORM Class: An object that represents a user
    """
    __tablename__ = "Users"
    id              = db.Column('id',           db.Integer,             primary_key=True)
    email           = db.Column('email',        db.String(128),         unique=True)
    passwordhash    = db.Column('passwordhash', db.String(128))

def __init__(self, email, password):
    self.email = email
    self.passwordhash = generate_password_hash(password)

    logging.info("creating user with email and pw:" + email + " " + password)

And when I create a new user:

newuser = User(email="test@email.com", password="hunter2")
db.session.add(newuser)

I get a KeyError: 140736669950912

  File "/usr/local/lib/python3.6/site-packages/sqlalchemy/util/_collections.py", line 988, in __call__
    return self.registry[key]
KeyError: 140736669950912

Where is this number coming from? I am also getting another error during the handling of that KeyError which is a RuntimeError: application not registered on db instance and no applicationbound to current context

  • Can you add the full stack trace, please. – univerio Jul 14 '17 at 08:31
  • This happens when i try to run Person.query.all() or any other select query in console. – Marcelo Fonseca Apr 17 '19 at 01:56
  • This error happens because the (scoped) session can't be found in the session registry. So the cause is probably trying to use Flask-SQLA sessions outside of a Flask context. See the [docs](https://flask-sqlalchemy.palletsprojects.com/en/2.x/contexts/) – snakecharmerb Sep 04 '22 at 09:30

2 Answers2

1

As recommended in their current documentation, instead of manually creating an instance of SqlAlchemy() and saving it to db, try inheriting from sqlalchemy.ext.declarative.Base, like so:

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine(DATABASE_URI, echo=False)
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))

Base = declarative_base()
Base.query = db_session.query_property()


# the following should be moved to your models.py

from sqlalchemy import Column, Integer, String

class User(Base):
    """ORM Class: An object that represents a user
    """
    __tablename__ = "Users"

    id = Column(Integer)
    email = Column(String(128), primary_key=True)
    passwordhash = Column(String(128), unique=True)
darksky
  • 1,955
  • 16
  • 28
  • 1
    Hi, any idea what's wrong with creating SqlAlchemy() instance manually? That's the approach used by many SQLAlchemy+Flask tutorials, hence I follow the same, but end up with the KeyError. – shiouming May 02 '18 at 11:11
  • @shiouming I remember it having to do with application context not being set properly. See https://stackoverflow.com/questions/19437883/when-scattering-flask-models-runtimeerror-application-not-registered-on-db-w – darksky May 08 '18 at 00:35
0

Checking whether your migrations are upto date might also help. When I faced this problem, it got fixed after I removed the existing and re-ran migrations:

python manage.py db migrate
python manage.py db ugrade
Srmsbrmnm
  • 149
  • 4