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

Custom error message json object with flask-restful

People tend to overuse abort(), while in fact it is very simple to generate your own errors. You can write a function that generates custom errors easily, here is one that matches your JSON: def make_error(status_code, sub_code, message, action): response = jsonify({ ‘status’: status_code, ‘sub_code’: sub_code, ‘message’: message, ‘action’: action }) response.status_code = status_code return … Read more

Error 99 connecting to localhost:6379. Cannot assign requested address

In the flask app I have a function that tries to create a redis client db = redis.Redis(host=”localhost”, port=6379, decode_responses=True) When your flask process runs in a container, localhost refers to the network interface of the container itself. It does not resolve to the network interface of your docker host. So you need to replace … Read more

Flask.url_for() error: Attempted to generate a URL without the application context being pushed

According to the doc: Setting a SERVER_NAME also by default enables URL generation without a request context but with an application context. since you’re using app_context, you may set the SERVER_NAME Configuration Value. By the way, as the doc:Adding a favicon says: <link rel=”shortcut icon” href=”https://stackoverflow.com/questions/31766082/{{ url_for(“static’, filename=”favicon.ico”) }}”> the above line should be enough … Read more

tech