sqlalchemy flush() and get inserted id?

I’ve just run across the same problem, and after testing I have found that NONE of these answers are sufficient. Currently, or as of sqlalchemy .6+, there is a very simple solution (I don’t know if this exists in prior version, though I imagine it does): session.refresh() So, your code would look something like this: … Read more

SQLAlchemy: cascade delete

The problem is that sqlalchemy considers Child as the parent, because that is where you defined your relationship (it doesn’t care that you called it “Child” of course). If you define the relationship on the Parent class instead, it will work: children = relationship(“Child”, cascade=”all,delete”, backref=”parent”) (note “Child” as a string: this is allowed when … Read more

sqlalchemy IS NOT NULL select

column_obj != None will produce a IS NOT NULL constraint: In a column context, produces the clause a != b. If the target is None, produces a IS NOT NULL. or use is_not()*: Implement the IS NOT operator. Normally, IS NOT is generated automatically when comparing to a value of None, which resolves to NULL. … Read more

Flask-SQLalchemy update a row’s information

Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.commit(). For example: admin = User.query.filter_by(username=”admin”).first() admin.email=”my_new_email@example.com” db.session.commit() user = User.query.get(5) user.name=”New Name” db.session.commit() Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as … Read more

How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

Most likely your application doesn’t use the Flask-SQLAlchemy event system, so you’re probably safe to turn off. You’ll need to audit the code to verify–you’re looking for anything that hooks into models_committed or before_models_committed. If you do find that you’re using the Flask-SQLAlchemy event system, you probably should update the code to use SQLAlchemy’s built-in … Read more