How Can I Automatically Populate SQLAlchemy Database Fields? (Flask-SQLAlchemy)

Just add server_default or default argument to the column fields: created_on = db.Column(db.DateTime, server_default=db.func.now()) updated_on = db.Column(db.DateTime, server_default=db.func.now(), server_onupdate=db.func.now()) I prefer the {created,updated}_on column names. ๐Ÿ˜‰ SQLAlchemy docs about column insert/update defaults. [Edit]: Updated code to use server_default arguments in the code. [Edit 2]: Replaced onupdate with server_onupdate arguments.

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