flask-sqlalchemy

flask-sqlalchemy or sqlalchemy

我的未来我决定 提交于 2019-11-30 10:10:39
问题 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. 回答1: The main feature of the Flask-SQLAlchemy is proper integration with Flask application - it creates

Flask and SQLAlchemy, application not registered on instance

岁酱吖の 提交于 2019-11-30 10:03:57
I'm currently trying to piece together a small Flask application. This is my structure. run.py application __init__.py database.py models.py views.py database.py contains just the SQLAlchemy object: db = SQLAlchemy() I then import this into my models.py to create my models. Lastly, inside __init__.py I import db from database.py and do: from .database import db from flask import Flask app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///application.db' db.init_app(app) db.create_all() However, I cannot create the tables from the models it appears. If I remove db.create_all(

Populating a SQLAlchemy many-to-many relationship using ID's instead of objects

有些话、适合烂在心里 提交于 2019-11-30 08:52:26
The situation: So, I have a basic many-to-many relationship in SQLAlchemy using an association table . For example, a person can attend many parties, and a party can have many persons as guests: class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) name = db.Column(db.String(50)) class SexyParty(Base): __tablename__ = 'sexy_party' id = Column(Integer, primary_key=True) guests = relationship('Person', secondary='guest_association', lazy='dynamic', backref='parties') guest_association = Table( 'guest_association', Column('user_id', Integer(), ForeignKey('person.id')

Is a SQLAlchemy query vulnerable to injection attacks?

五迷三道 提交于 2019-11-30 08:36:12
问题 I have the following query that uses like to search a blog. I am not sure if I'm making myself vulnerable to a SQL injection attack if I do this. How is SQLAlchemy handling this? Is it safe? search_results = Blog.query.with_entities(Blog.blog_title).filter(Blog.blog_title.like("%"+ searchQuery['queryText'] +"%")).all() 回答1: The underlying db-api library for whatever database you're using (sqlite3, psycopg2, etc.) escapes parameters. SQLAlchemy simply passes the statement and parameters to

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

a 夏天 提交于 2019-11-30 08:24:17
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 self.date_out = datetime.now() return self def checkinRecord(self,uid): self.editing_uid = uid self

Alembic: alembic revision says Import Error

拟墨画扇 提交于 2019-11-30 08:22:07
I am trying to integrate my Flask project with Alembic My application structure looks like project/ configuration/ __init__.py dev.py test.py core/ # all source code db/ migrations/ __init__.py alembic.ini env.py versions/ When I try to run the following from my db directory, I see File "migration/env.py", line 55, in run_migrations_online from configuration import app, db ImportError: No module named configuration I tried the solution mentioned in Request a simple alembic working example for Auto Generating Migrations , but it does not work for me My method in env.py run_migrations_online()

Flask-Migrate not creating tables

痴心易碎 提交于 2019-11-30 08:09:56
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) created_at = db.Column(db.DateTime, nullable=False) compressed_csv = db.Column(db.LargeBinary) def __init

SQLAlchemy - Self referential Many-to-many relationship with extra column

感情迁移 提交于 2019-11-30 07:24:40
I have a model representing user and I want to create a relationship between users representing that they are friends. My functional model with association table and methods to list all the friends look like this friendship = db.Table('friend', db.Column('id', db.Integer, primary_key=True), db.Column('fk_user_from', db.Integer, db.ForeignKey('user.id'), nullable=False), db.Column('fk_user_to', db.Integer, db.ForeignKey('user.id'), nullable=False) ) class User(db.Model): ... ... friends = db.relationship('User', secondary=friendship, primaryjoin=(friendship.c.fk_user_from==id), secondaryjoin=

What's your folder layout for a Flask app divided in modules?

筅森魡賤 提交于 2019-11-30 07:06:33
问题 I am experimenting with Flask coming from Django and I really like it. There is just one problem that I ran into. I read the flask docs and the part about big applications or something like that and it explains a way to divide your project in packages, each one with its own static and templates folder as well as its own views module. the thing is that I cannot find a way that works to put the models in there using SQLAlchemy with the Flask extension. It works from the interactive prompt to

SQLAlchemy - Self referential Many-to-many relationship with extra column

情到浓时终转凉″ 提交于 2019-11-30 07:01:15
问题 I have a model representing user and I want to create a relationship between users representing that they are friends. My functional model with association table and methods to list all the friends look like this friendship = db.Table('friend', db.Column('id', db.Integer, primary_key=True), db.Column('fk_user_from', db.Integer, db.ForeignKey('user.id'), nullable=False), db.Column('fk_user_to', db.Integer, db.ForeignKey('user.id'), nullable=False) ) class User(db.Model): ... ... friends = db