flask-sqlalchemy

Using Flask-SQLAlchemy in Blueprint models without reference to the app [closed]

末鹿安然 提交于 2019-11-29 19:25:12
I'm trying to create a "modular application" in Flask using Blueprints. When creating models, however, I'm running into the problem of having to reference the app in order to get the db -object provided by Flask-SQLAlchemy. I'd like to be able to use some blueprints with more than one app (similar to how Django apps can be used), so this is not a good solution.* It's possible to do a switcharoo, and have the Blueprint create the db instance, which the app then imports together with the rest of the blueprint. But then, any other blueprint wishing to create models need to import from that

flask-sqlalchemy or sqlalchemy

十年热恋 提交于 2019-11-29 19:14:55
I am new in both flask and sqlalchemy, I just start working on a flask app, and I am using sqlalchemy for now. I was wondering if there is any significant benefit I can get from using flask-sqlalchemy vs sqlalchemy. I could not find enough motivations in http://packages.python.org/Flask-SQLAlchemy/index.html or maybe I did not understand the value!! I would appreciate your clarifications. The main feature of the Flask-SQLAlchemy is proper integration with Flask application - it creates and configures engine, connection and session and configures it to work with the Flask app. This setup is

flask admin : sqlalchemy.exc.InterfaceError(Error binding parameter 8)

末鹿安然 提交于 2019-11-29 18:17:46
I was trying to save a project's reviewer using below, and the select field shows correct: # Query the user with Role.id == 4 as reviewer def reviewer_choices(): return User.query.join(User.roles).filter(Role.id == 4) # Build a select field class ProjectView(sqla.ModelView): form_extra_fields = { 'reviewer': sqla.fields.QuerySelectField( label='Reviewer', query_factory=reviewer_choices, )} However, when I was trying to save it, the error occurred: InterfaceError: (sqlite3.InterfaceError) Error binding parameter 8 - probably unsupported type. [SQL: u'INSERT INTO project(...reviewer...)VALUES(..

SQLAlchemy: show only latest result if a join returns multiple results

自作多情 提交于 2019-11-29 18:09:05
I'm trying to create a Flask app that shows the latest score from individual players. So a player can have multiple scores, but on the leaderboard I only want to show her most recent score. My models.py: class Player(db.Model): __tablename__ = 'player' id = db.Column(db.Integer, primary_key=True) firstname = db.Column(db.String, nullable=False) score = db.relationship('Score', backref='player', lazy='dynamic') def __init__(self, firstname): self.firstname = firstname def __repr__(self): return '<id {}>'.format(self.id) class Score(db.Model): __tablename__ = 'score' id = db.Column(db.Integer,

Could not assemble any primary key columns for mapped table

◇◆丶佛笑我妖孽 提交于 2019-11-29 15:56:22
问题 When I'm trying to create a database schema migration, I'm getting this weird error. Can you please help me to figure out what's wrong? $ python app.py db upgrade [skipped] sqlalchemy.exc.ArgumentError: Mapper Mapper|EssayStateAssociations|essay_associations could not assemble any primary key columns for mapped table 'essay_associations' My model: class EssayStateAssociations(db.Model): __tablename__ = 'essay_associations' application_essay_id = db.Column( db.Integer, db.ForeignKey(

How to call stored procedure with SQLAlchemy that requires a user-defined-type Table parameter

旧时模样 提交于 2019-11-29 15:10:10
I have a stored procedure on MSSQL server, "prc_add_names", that takes a table-value parameter. The parameter itself is of a custom type "StringTable" defined like so: CREATE TYPE [dbo].[StringTable] AS TABLE([strValue] [nvarchar](max) NULL) I have no idea how to execute this procedure using SQLAlchemy. I am used to calling procedures with arguments using session.execute like this: result = session.execute('prc_do_something :pArg', {pArg:'foo'}) However, this does not work if I simply pass a list of strings as the argument: result = session.execute('prc_add_names :pArg', {pArg: ['Name One',

How to connect to mysql server with SSL from a flask app

扶醉桌前 提交于 2019-11-29 14:14:06
I want to connect to a mysql server via flask and app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/schema' How can I add ca-cert, client-key and client-cert to the connection? Thomas N. You can add theses informations in your URI like this : app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://your-username:your-password@localhost/schema?ssl_key=MyCertFolder/client-key.pem&ssl_cert=MyCertFolder/client-cert.pem' Or using the SQLAlchemy create_engine, take a look to this post In case you need to use a Cloud-based MySQL service, this configuration will be

Can I inspect a sqlalchemy query object to find the already joined tables?

回眸只為那壹抹淺笑 提交于 2019-11-29 13:11:18
问题 I'm trying to programmatically build a search query, and to do so, I'm joining a table. class User(db.Model): id = db.Column(db.Integer(), primary_key=True) class Tag(db.Model): id = db.Column(db.Integer(), primary_key=True) user_id = db.Column(db.Integer(), db.ForeignKey('user.id')) title = db.Column(db.String(128)) description = db.Column(db.String(128)) This is a bit of a contrived example - I hope it makes sense. Say my search function looks something like: def search(title_arg, desc_arg)

sqlalchemy flask: AttributeError: 'Session' object has no attribute '_model_changes' on session.commit()

时光毁灭记忆、已成空白 提交于 2019-11-29 11:34:47
问题 I've seen a lot of problems with SessionMaker, but this one is slightly different. Not sure why, but sqlalchemy won't let my session object commit. In my app, I have some code that does: views.py rec = session.query(Records).filter(Records.id==r).first() n = rec.checkoutRecord(current_user.id) session.add(n) session.commit() models.py: class Records(UserMixin, CRUDMixin, Base): __table__ = Table('main_records', Base.metadata, autoload=True) def checkoutRecord(self,uid): self.editing_uid = uid

Flask-Migrate not creating tables

爱⌒轻易说出口 提交于 2019-11-29 11:06:21
问题 I have the following models in file listpull/models.py : from datetime import datetime from listpull import db class Job(db.Model): id = db.Column(db.Integer, primary_key=True) list_type_id = db.Column(db.Integer, db.ForeignKey('list_type.id'), nullable=False) list_type = db.relationship('ListType', backref=db.backref('jobs', lazy='dynamic')) record_count = db.Column(db.Integer, nullable=False) status = db.Column(db.Integer, nullable=False) sf_job_id = db.Column(db.Integer, nullable=False)