flask-sqlalchemy

Why isn't SQLAlchemy translating this object generated by a FactoryBoy SubFactory into a foreign key?

╄→гoц情女王★ 提交于 2019-12-07 08:41:09
问题 I'm using Flask and SQLAlchemy (via the Flask-SQLAlchemy extension) together with Factory_Boy. My GearItem model has a foreign key to GearCategory . Factory_Boy handles this through the SubFactory function that creates the object to be used as the foreign key in the original factory. Here are my model definitions: class GearCategory(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Text, unique=True, nullable=False) gear_items = db.relationship('GearItem', backref=

Using multiple POSTGRES databases and schemas with the same Flask-SQLAlchemy model

怎甘沉沦 提交于 2019-12-07 07:30:52
问题 I'm going to be very specific here, because similar questions have been asked, but none of the solutions work for this problem. I'm working on a project that has four postgres databases, but let's say for the sake of simplicity there are 2. Namely, A & B A,B represent two geographical locations, but the tables and schemas in the database are identical. Sample model: from flask_sqlalchemy import SQLAlchemy from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base db =

Flask-SQLAlchemy TimeoutError

霸气de小男生 提交于 2019-12-07 07:30:36
问题 My backend configuration is : Ubuntu 12.04 Python 2.7 Flask 0.9 Flask-SQLAlchemy Postgres 9.2 I've got this error message: TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30 Do I need to close explicitly the db.session? Shouldn't be the connection back to pool when session goes out of scope? 回答1: This could happen if you are using debug=True in your application and you have loaded several pages or API endpoints which errored out. The reason is that

SQLAlchemy: __init__() takes 1 positional argument but 2 were given (many to many)

依然范特西╮ 提交于 2019-12-07 06:36:52
问题 SQLAlchemy is undoubtedly very powerful, but the documentation implicitly assumes lots of prior knowledge and on the subject of relationships, mixes between the backref and the newly-preferred back_populates() methods, which I find very confusing. The following model design is pretty much an exact mirror of the guide in the documentation that deals with the Association Objects for many-to-many relationships. You can see that the comments are still identical to those in the original article,

How to select_related() in Flask/SQLAlchemy?

…衆ロ難τιáo~ 提交于 2019-12-07 04:46:54
问题 Having following models: class Question(db.Model): id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(125)) text = db.Column(db.Text()) answers = db.relationship('Answer', backref='for_question') class Answer(db.Model): id = db.Column(db.Integer(), primary_key=True) text = db.Column(db.Text()) question_id = db.Column(db.Integer(), db.ForeignKey('question.id')) How can I perform select_related in SQLAlchemy/Flask? I've found in documentation that I can make something

flask sqlalchemy column constraint for positive integer

旧城冷巷雨未停 提交于 2019-12-07 04:17:02
问题 how can i define a column as a positive integer using flask sqlalchemy? i am hoping the answer would look something like this: class City(db.Model): id = db.Column(db.Integer, primary_key=True) population = db.Column(db.Integer, positive=True) def __init__(self,population): self.population = population however, this class definition will throw an error b/c sqlalchemy does not know about a 'positive' argument. i could raise an exception if an object is instantiated with a negative value for

Commiting a transaction from a PostgreSQL function in flask

馋奶兔 提交于 2019-12-07 03:42:05
问题 I'm a newbie to Flask and SQLAlchemy (been working with Django for the last 3 years). I need to call an existing PostgreSQL function that writes to 3 different tables in a database. This is out of my control (I just have to make it work). The function returns a record (custom Postgres type) with information about the results. Here is the code: from flask.ext.sqlalchemy import SQLAlchemy db = SQLAlchemy() ... retval = db.engine.execute( 'SELECT * FROM add_purchase(%s, %s, %s, %s, %s, %s, %s);'

How to set one to many and one to one relationship at same time in Flask-SQLAlchemy?

南笙酒味 提交于 2019-12-07 02:03:59
问题 I'm trying to create one-to-one and one-to-many relationship at the same time in Flask-SQLAlchemy. I want to achieve this: "A group has many members and one administrator." Here is what I did: class Group(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(140), index=True, unique=True) description = db.Column(db.Text) created_at = db.Column(db.DateTime, server_default=db.func.now()) members = db.relationship('User', backref='group') admin = db.relationship(

flask-admin not showing foreignkey columns

拈花ヽ惹草 提交于 2019-12-06 22:08:22
问题 class Parent(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(120)) def __repr_(self): return '<Parent %r>' % (self.name) admin.add_view(ModelView(Parent, db.session)) class Child(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(120)) parent = db.Column(db.Integer, db.ForeignKey(Parent)) admin.add_view(ModelView(Child, db.session)) Hello - The code above is an example of flask-admin page that I am trying to create. The

SQLAlchemy Object already attached to session

ぐ巨炮叔叔 提交于 2019-12-06 18:09:34
问题 I'm trying to get a server for an app working, but I'm getting an error upon login: [!] Object '<User at 0x7f12bc185a90>' is already attached to session '2' (this is '3') It seems the session I'm adding is already on the database. This is the snippet of code that is causing the problem: @app.route('/login', methods=['POST']) def login(): u = User.query.filter(User.username == request.form["username"]).first() if not u or u.password != request.form["password"]: return error("E1") s = Session