flask-sqlalchemy

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

送分小仙女□ 提交于 2019-12-05 12:27:47
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='category', lazy='dynamic', order_by='GearItem.name') class GearItem(db.Model): id = db.Column(db

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

左心房为你撑大大i 提交于 2019-12-05 11:40:27
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, and I've only changed the actual code. class MatchTeams(db.Model): match_id = db.Column(db.String, db

How to select_related() in Flask/SQLAlchemy?

最后都变了- 提交于 2019-12-05 11:00:32
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 like session.query(Question).options(joinedload(Question.aswers)) But I need first to get specific

Commiting a transaction from a PostgreSQL function in flask

梦想的初衷 提交于 2019-12-05 08:23:03
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);', clean_data.account_id, amount, tax, deposit, clean_data.pos_id, g.token_id, clean_data.external

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

£可爱£侵袭症+ 提交于 2019-12-05 05:51:51
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('User', backref='admin_group', uselist=False) def __repr__(self): return '<Group %r>' % (self.name) class

Self-Referential Association Relationship SQLalchemy

寵の児 提交于 2019-12-05 05:38:05
问题 In my flask application with flask-sqlalchemy i need to create association between two contact here is my Contact model class Contact(db.Model): __tablename__ = 'contact' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.Unicode(120), nullable=False, unique=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) to_contacts = db.relationship('Contact', secondary='ContactRelation', primaryjoin='id==contactrelation.c.from_contact_id', secondaryjoin='id==contactrelation.c

sqlalchemy join with sum and count of grouped rows

故事扮演 提交于 2019-12-05 04:21:05
Hi i am working on a little prediction game in flask with flask-sqlalchemy I have a User Model: class User(db.Model, UserMixin): id = db.Column(db.Integer, primary_key=True) nick = db.Column(db.String(255), unique=True) bets = relationship('Bet', backref=backref("user")) and my Bet model class Bet(db.Model): id = db.Column(db.Integer, primary_key=True) uid = db.Column(db.Integer, db.ForeignKey('user.id')) matchid = db.Column(db.Integer, db.ForeignKey('match.id')) points = db.Column(db.Integer) Both are not the full classes but it should do it for the question. A user can gather points for

Flask and SQLAlchemy and the MetaData object

浪尽此生 提交于 2019-12-05 04:01:17
问题 it's the first time i am using this environment. The part of SQLAlchemy i am willing to use is just the one that allows me to query the database using Table objects with autoload = True. I am doing this as my tables already exist in the DB (mysql server) and were not created by defining flask models. I have gone through all the documentation and i don't seem to find an answer. Here is some code: app = Flask(__name__) app.config.from_object(__name__) metadata = None def connect_db(): engine =

SQLAlchemy group_concat and duplicates

南笙酒味 提交于 2019-12-05 03:26:33
When I try to join a many-to-many table and group it by the main-id I am getting duplicates when I add the second many-to-many table. Here is how my models look like: Models user class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) user_fistName = db.Column(db.String(64)) ... student_identifier student_identifier = db.Table('student_identifier', db.Column('class_id', db.Integer, db.ForeignKey('class.class_id')), db.Column('id', db.Integer, db.ForeignKey('user.id')) ) class class Class(db.Model): sqlite_autoincrement=True class_id = db.Column(db.Integer, primary_key

Run function after a certain type of model is committed

眉间皱痕 提交于 2019-12-05 03:02:18
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 notify_subscribers once again. No matter which option you chose below, SQLAlchemy comes with a big warning