What is the difference between .one() and .scalar()

SQLAlchemy has nice documentation. one() Return exactly one result or raise an exception. Raises sqlalchemy.orm.exc.NoResultFound if the query selects no rows. Raises sqlalchemy.orm.exc.MultipleResultsFound if multiple object identities are returned, or if multiple rows are returned for a query that returns only scalar values as opposed to full identity-mapped entities. Link on one() method scalar() Return … Read more

CommandError: Can’t locate revision identified by ‘…’ when migrating using Flask-Migrate

you delete the migration directory but the version has been saved in the database, so you have to delete the version info in the dabase, run delete from alembic_version; in mysql shell. As suggested by @mirekphd, If this is a developing environment or a single app for test, just delete it, Else BACKUP the data … Read more

Flask-SQLAlchemy Constructor

In most cases not defining a constructor in your model class gives you the correct behavior. Flask-SQLAlchemy’s base model class (which is also SQLAlchemy’s declarative base class) defines a constructor that just takes **kwargs and stores all the arguments given, so it isn’t really necessary to define a constructor. If you do need to define … Read more

flask-sqlalchemy delete query failing with “Could not evaluate current criteria in Python”

You need to use one of options for bulk delete Stock.query.filter(Stock.ticker.in_(new_tickers)).delete(synchronize_session=False) Stock.query.filter(Stock.ticker.in_(new_tickers)).delete(synchronize_session=’evaluate’) Stock.query.filter(Stock.ticker.in_(new_tickers)).delete(synchronize_session=’fetch’) Basically, SQLAlchemy maintains the session in Python as you issue various SQLAlchemy methods. When you delete entries, how will SQLAlchemy remove any removed rows from the session? This is controlled by a parameter to the delete method, “synchronize_session”. synchronize_session has three possible: … Read more

Invalid transaction persisting across requests

Edit 2016-06-05: A PR that solves this problem has been merged on May 26, 2016. Flask PR 1822 Edit 2015-04-13: Mystery solved! TL;DR: Be absolutely sure your teardown functions succeed, by using the teardown-wrapping recipe in the 2014-12-11 edit! Started a new job also using Flask, and this issue popped up again, before I’d put … Read more

Flask/SQLAlchemy – Difference between association model and association table for many-to-many relationship?

My apologies, I finally stumbled across the answer in the SQLAlchemy docs… https://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#many-to-many …where they explicitly define the difference: Many to Many adds an association table between two classes. association_table = Table(‘association’, Base.metadata, Column(‘left_id’, Integer, ForeignKey(‘left.id’)), Column(‘right_id’, Integer, ForeignKey(‘right.id’)) ) The association object pattern is a variant on many-to-many: it’s used when your association table … Read more

Alembic –autogenerate producing empty migration

As per @zzzeek, after I included the following in my env.py, I was able to work with –autogenerate option in env.py under run_migrations_online() from configuration import app from core.expense.models import user # added my model here alembic_config = config.get_section(config.config_ini_section) alembic_config[‘sqlalchemy.url’] = app.config[‘SQLALCHEMY_DATABASE_URI’] engine = engine_from_config( alembic_config, prefix=’sqlalchemy.’, poolclass=pool.NullPool) then I ran alembic revision –autogenerate -m … Read more

Defining SQLAlchemy enum column with Python enum raises “ValueError: not a valid enum”

The column type should be sqlalchemy.types.Enum. You’re using the Python Enum type again, which was valid for the value but not the column type. Back in 2016, when the question was written, this was necessary: class MyTable(db.Model): id = db.Column(db.Integer, primary_key = True) fruit_type = db.Column(db.Enum(FruitType)) However, this is not the case anymore.

SQLAlchemy: Hybrid expression with relationship

A much simpler approach for a simple case like this is an association proxy: class Teacher(db.Model): school_name = associationproxy(‘school’, ‘name’) This supports querying (at least with ==) automatically. I’m curious how the hybrid select() example didn’t work for you, since that’s the easiest way to fix this within a hybrid. And for the sake of … Read more