Update row (SQLAlchemy) with data from marshmallow

UPDATED, 2022-12-08 Extending the ModelSchema from marshmallow-sqlalchemy instead of Flask-Marshmallow you can use the load method, which is defined like this: load(data, *, session=None, instance=None, transient=False, **kwargs) Putting that to use, it should look like that (or similar query): node_schema.load(json_data, session= current_app.session, instance=Node().query.get(node_id)) And if you want to load without all required fields of Model, … Read more

How does `nullable=False` work in SQLAlchemy

The doc you reference explains the issue: This parameter is only used when issuing CREATE TABLE statements. If you originally created your database without nullable=False (or created it in some other way, separate from SQLAlchemy, without NOT NULL on that column), then your database column doesn’t have that constraint information. This information lives in reference … Read more

AttributeError: can’t set attribute when connecting to sqlite database with flask-sqlalchemy

Edit If you’re experiencing this, upgrading Flask-SQLAlchemy to >= 2.5 should resolve the issue per https://github.com/pallets/flask-sqlalchemy/issues/910#issuecomment-802098285. Pinning SQLAlchemy to ~1.3 should no longer be necessary. I ran into this issue a little earlier, but think I’ve figured out what’s going on. SQLAlchemy is automatically installed as a dependency for Flask-SQLAlchemy and its latest release (1.4.0) … Read more

How do ‘primaryjoin’ and ‘secondaryjoin’ work for many-to-many relationship in SQLAlchemy?

In a many to many relationship, the primaryjoin expression describes the join between the left table and the junction table, and the secondaryjoin describes the join between the junction table and the right table. In other words, the primaryjoin expression is saying, “find all rows in the followers table where follower_id is X”, the secondaryjoin … Read more

How to use count() in Flask-sqlalchemy

None of the given answers address flask-sqlalchemy specifically, where you would use exactly the example you gave: Table.query.filter_by(condition).count() You can perform .count() without filters: Table.query.count() You can also count using M2M relationships: ParentTable.children.count() And you can use any of these directly in your jinja templates like: {{ Table.query.filter_by(condition).count() }} Bonus points (for the performance minded): … Read more

Setting delete-orphan on SQLAlchemy relationship causes AssertionError: This AttributeImpl is not configured to track parents

OK in this case, you need to look more closely, though there is a warning here that likely should become an exception, and I’ll look into that. Here’s a working version of your example: from sqlalchemy.ext.associationproxy import association_proxy from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base= declarative_base() tagging = Table(‘tagging’,Base.metadata, … Read more

Share sqlalchemy models between flask and other apps

The Flask-SQLAlchemy extension, like most Flask extensions, should be created outside the factory, then initialized in the factory using init_app. This is so that you can use the db object before an app is created. db = SQLAlchemy() def create_app(): app = Flask(__name__) db.init_app(app) return app Your Flask app, like any properly designed Python project, … Read more

sqlalchemy filter by json field

Flask-SQLAlchemy’s SQLAlchemy object – commonly named db – gives access to functions etc. from sqlalchemy and sqlalchemy.orm, and so db.JSON is the generic JSON type that does not provide the Postgresql specific operators. You should instead use sqlalchemy.dialects.postgresql.JSON: from sqlalchemy.dialects.postgresql import JSON class Example(db.Model): id = db.Column(db.Integer(), nullable=False, primary_key=True, ) json_field = db.Column(JSON) With the … Read more

tech