flask-sqlalchemy

How to enforce constraints in `flask-admin`?

点点圈 提交于 2019-12-10 11:38:51
问题 We're using http://flask-admin.readthedocs.org/ for a quick admin interface. Our model has constraints defined as follows: __table_args__ = ( db.UniqueConstraint(user_id, role, domain_id), db.UniqueConstraint(user_id, role, customer_id), ) When saving a record that violates a constraint while in debug mode, the app stops with a traceback. If not in debug mode, it reports the error in a flash message and rolls back the transaction. This is the desired behaviour (i.e. flash message and rollback

Sqlalchemy one to many relationship join?

有些话、适合烂在心里 提交于 2019-12-10 09:58:59
问题 I am trying to do a simple join query like this, SELECT food._id, food.food_name, food_categories.food_categories FROM food JOIN food_categories ON food.food_category_id = food_categories._id but keep receiving an error. Here is how my classes are setup. class Food_Categories(db.Model): __tablename__ = 'food_categories' _id = db.Column(db.Integer, primary_key=True) food_categories = db.Column(db.String(30)) class Food(db.Model): __tablename__ = 'food' _id = db.Column(db.Integer, primary_key

How to get Flask-SQLAlchemy object to load relationship children for Jinja template?

谁说我不能喝 提交于 2019-12-10 04:05:11
问题 I have basic models for User and Post. In my User model, I have posts = db.relationship('Post', backref='user', lazy='dynamic') However, when I do something like return render_template('user.html', users=users) I want to do stuff like {% for user in users %} <tr> <td>{{ user.id }}</td> <td>{{ user.posts|length }}</td> </tr> {% endfor %} Unfortunately, this doesn't work. Posts is a query, not an object b/c of lazy='dynamic' . I can do the above if I change lazy='joined' , but then it would be

Run function after a certain type of model is committed

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-10 03:00:47
问题 I want to run a function when instances of the Post model are committed. I want to run it any time they are committed, so I'd rather not explicitly call the function everywhere. How can I do this? def notify_subscribers(post): """ send email to subscribers """ ... post = Post("Hello World", "This is my first blog entry.") session.commit() # How to run notify_subscribers with post as argument # as soon as post is committed successfully? post.title = "Hello World!!1" session.commit() # Run

SQLAlchemy / Flask / PostgreSQL pool connection

不羁的心 提交于 2019-12-10 01:26:57
问题 After having played for a long time with Django, I'm trying a bit of Flask with SQLAlchemy, and I must say I quite like it. However there is something that I don't get: I have a small Flask / SQLAlchemy app that uses PostgreSQL. In my __init__.py file I have: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_object('settings') db = SQLAlchemy(app) I wanted to know: Is there automatic connection pooling ? Or does every call to Model.query

Flask foreign_keys still shows AmbiguousForeignKeysError

人盡茶涼 提交于 2019-12-09 18:08:12
问题 I have two foreign keys in an entity refering to another entity. Here is how it looks class Review(db.Model): __tablename__ = 'Review' id = db.Column(db.Integer, primary_key = True) user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False) business_user_id = db.Column(db.Integer, db.ForeignKey('User.id'), nullable=False) user = db.relationship('User', foreign_keys=[user_id]) business_user = db.relationship('User', foreign_keys=[business_user_id]) and class User(db.Model): _

SqlAlchemy update not working with Sqlite

妖精的绣舞 提交于 2019-12-09 15:55:35
问题 I followed the (two) examples in this question: SQLAlchemy: a better way for update with declarative? And I'm finding that a model update does not happen when using sqlite with flask-sqlalchemy on Ubuntu Linux. The very simplest example doesn't work for me: class Task: id= db.Column(db.Integer, primary_key=True) name= db.Column(db.String(32), unique=True) desc= db.Column(db.String(255), unique=False) state= db.Column(db.Boolean) # ... @app.route("/task/<int:id>/update",methods=["POST"]) def

db.Model vs db.Table in Flask-SQLAlchemy [closed]

折月煮酒 提交于 2019-12-09 12:22:22
问题 Closed . This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 2 years ago . The Flask-SQLAlchemy docs say that many-to-many lookup tables should not subclass db.Model but instead be written as db.Tables. From the docs: If you want to use many-to-many relationships you will need to define a helper table that is used for the relationship. For this

flask-sqlalchemy with dynamic database connections

牧云@^-^@ 提交于 2019-12-09 07:09:51
问题 I have one main db, in which each client's own db connection is stored. So every client works with 2 db: main and its own db, connection of which must be decided for each http call. How can I do this using flask-sqlalchemy extension, or may be purely in sqlalchemy? 回答1: You can handle multiple sessions in Flask-SQLalchemy: engine = create_engine(DATABASE_URI) db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine)) db_session.query...() and engine2 = create

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table

我怕爱的太早我们不能终老 提交于 2019-12-09 06:07:38
问题 I am fresh to flask and was trying to build a blog on my own, and I ran into an issue with SQLite operation error. I have researched similar problem on Github and Stackoverflow but none of the typical typo or error in old questions happens to me. It would be appreciated and really great if anyone can help me because this problem is like killing me and already cost me two days, I feel really bad. In the code I have defined the table name which is "users_table" and run "db.create_all()" at the