flask-sqlalchemy

How to update parent table timestamp when child table is modified?

Deadly 提交于 2019-12-01 09:40:22
How can I update parent timestamp when child table is modified? I would like to use parent table timestamp for checking if my rest client should update it's local version of these tables. class Parent(db.Model): id = db.Column(db.Integer, primary_key=True) version = db.Column(db.Integer) timestamp = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) childs = db.relationship('Children', backref='parent', lazy='dynamic', cascade="all, delete-orphan") class Children(db.Model): id = db.Column(db.Integer, primary_key=True) version = db.Column(db.Integer) timestamp = db.Column

How to update parent table timestamp when child table is modified?

笑着哭i 提交于 2019-12-01 08:08:05
问题 How can I update parent timestamp when child table is modified? I would like to use parent table timestamp for checking if my rest client should update it's local version of these tables. class Parent(db.Model): id = db.Column(db.Integer, primary_key=True) version = db.Column(db.Integer) timestamp = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow) childs = db.relationship('Children', backref='parent', lazy='dynamic', cascade="all, delete-orphan") class Children(db

Flask app getting error of “could not locate flask application. …FLASK_APP environment variable” for Flask Migrate

只愿长相守 提交于 2019-12-01 07:44:18
问题 I have a file db_table.py that looks like: from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/mydb' db = SQLAlchemy(app) migrate = Migrate(app, db) .....db tables.... When I try to run: flask db init I get: Error: Could not locate Flask application. You did not provide the FLASK_APP environment variable. I tried first manually setting FLASK_APP var, by

flask-admin: how to allow only super users can view the specified table column?

纵饮孤独 提交于 2019-12-01 07:32:45
问题 I've built an app with a table called Project which is stored in sqlite, I want allow only super users can view the approve column when creating, editing data. The Project data is retrieved in the "class Project" , and I've added if current_user.has_role('superuser') in "class ProjectView" : from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_security import SQLAlchemyUserDatastore, current_user,UserMixin, RoleMixin from flask_admin.contrib import sqla # Create Flask

Deleting from many-to-many SQL-Alchemy and Postgresql

北战南征 提交于 2019-12-01 06:41:25
I'm trying to delete a child object from a many-to-many relationship in sql-alchemy. I keep getting the following error: StaleDataError: DELETE statement on table 'headings_locations' expected to delete 1 row(s); Only 2 were matched. I have looked at a number of the existing stackexchange questions ( SQLAlchemy DELETE Error caused by having a both lazy-load AND a dynamic version of the same relationship , SQLAlchemy StaleDataError on deleting items inserted via ORM sqlalchemy.orm.exc.StaleDataError , SQLAlchemy Attempting to Twice Delete Many to Many Secondary Relationship , Delete from Many

Deleting from many-to-many SQL-Alchemy and Postgresql

时间秒杀一切 提交于 2019-12-01 05:15:28
问题 I'm trying to delete a child object from a many-to-many relationship in sql-alchemy. I keep getting the following error: StaleDataError: DELETE statement on table 'headings_locations' expected to delete 1 row(s); Only 2 were matched. I have looked at a number of the existing stackexchange questions (SQLAlchemy DELETE Error caused by having a both lazy-load AND a dynamic version of the same relationship, SQLAlchemy StaleDataError on deleting items inserted via ORM sqlalchemy.orm.exc

Flask-Restless dumps Decimal value from Flask-Sqlalchemy

北战南征 提交于 2019-12-01 04:03:38
问题 i have this model using Flask-SQLAlchemy: class Menu(Document, db.Model): id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(80), unique=True, index=True) price = db.Column(db.Numeric) and i can create api using Flask-Restless for this model. The problem is when i HTTP GET from the api url: File "/usr/lib/python2.6/json/encoder.py", line 344, in default raise TypeError(repr(o) + " is not JSON serializable") TypeError: Decimal('10000.0000000000') is not

PostgreSQL ILIKE query with SQLAlchemy

不问归期 提交于 2019-12-01 02:40:55
I'd like to run a query that selects all posts, case insensitive, that have titles that match '%' + [some_phrase] + '%' . That is, select all rows that have titles that contain some phrase, case insensitive. From the research I've done, it looks like I need to use Postgres's ILIKE query for it to match case insensitive. How can I execute a query like this with SQLAlchemy? class Post(db.Model): id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(250)) content = db.Column(db.String(5000)) I think it should work Post.query.filter(Post.title.ilike('%some_phrase%')) http:/

Flask DateTime won't set with default datetime.utcnow [duplicate]

本小妞迷上赌 提交于 2019-12-01 02:36:23
问题 This question already has answers here : How Can I Automatically Populate SQLAlchemy Database Fields? (Flask-SQLAlchemy) (2 answers) Closed last year . I am new to SQLAlchemy and have been unable to set the DateTime for created. I have tried using the "default" option found in many examples. I have also tried setting it manually (I have it commented out). Neither have worked so far. Any help would be appreciated. models.py import datetime from flask.ext.sqlalchemy import SQLAlchemy from

unresolved attribute “Column” in class “SQLAlchemy”

馋奶兔 提交于 2019-12-01 02:02:27
问题 i use pycharm 5.0 and python3.5.And i download all the liarbry by the build-in function of pycharm(setting-project-project interpreter-"+").other libraries appear well,but some problems happens to flask-SQLAlchemy. i import flask-SQLAlchemy successfully.however,pycharm remind me that "unresolved attribute reference 'Column' in class'SQLAlchemy'"."unresolved attribute reference 'relationship' in class 'SQLAlchemy'" and so on. I have try some ways ,but they didn't work.for example:1.restart 2