flask-sqlalchemy

Alembic --autogenerate producing empty migration

杀马特。学长 韩版系。学妹 提交于 2019-11-29 01:24:22
I am trying to use Alembic for the first time and want to use --autogenerate feature described here My project structure looks like project/ configuration/ __init__.py dev.py test.py core/ app/ models/ __init__.py user.py db/ alembic/ versions/ env.py alembic.ini I am using Flask and SQLAlchemy and their Flask-SQLAlchemy extension. my model User looks like class User(UserMixin, db.Model): __tablename__ = 'users' # noinspection PyShadowingBuiltins uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True, unique=True) email = Column('email', String, nullable=False, unique=True)

PyCharm resolving - flask.ext.sqlalchemy vs flask_sqlalchemy

时光怂恿深爱的人放手 提交于 2019-11-28 21:14:11
If I use the following format in my application, everything works, except PyCharms resolving / autocomplete feature: from flask.ext.sqlalchemy import SQLAlchemy If I use the following format in my application, everything works. But, alas, it is not the correct way to import the libraries: from flask_sqlalchemy import SQLAlchemy Is there any way to make PyCharm resolve the first syntax correctly? The flask.ext namespace is a transistion namespace, see the Extension Import Transition section of the Flask Extension Development docs: For a while we recommended using namespace packages for Flask

How to use flask-sqlalchemy with existing sqlalchemy model?

假装没事ソ 提交于 2019-11-28 20:45:50
I've read flask-sqlalchemy or sqlalchemy which recommends use of flask-sqlalchemy with flask. I want to follow this approach. However, I have an existing model written for command line scripts which is based on sqlalchemy's declarative_base, e.g., from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() # create sqlalchemy Base class : class Runner(Base): etc. I want to still be able to use the command line scripts with this model, but also want to build a web application around the model. Is there a way to extend the existing model, to gain the benefit of using the

SQLAlchemy - performing a bulk upsert (if exists, update, else insert) in postgresql [duplicate]

自古美人都是妖i 提交于 2019-11-28 18:38:58
This question already has an answer here: How to UPSERT (MERGE, INSERT … ON DUPLICATE UPDATE) in PostgreSQL? 6 answers I am trying to write a bulk upsert in python using the SQLAlchemy module (not in SQL!). I am getting the following error on a SQLAlchemy add: sqlalchemy.exc.IntegrityError: (IntegrityError) duplicate key value violates unique constraint "posts_pkey" DETAIL: Key (id)=(TEST1234) already exists. I have a table called posts with a primary key on the id column. In this example, I already have a row in the db with id=TEST1234 . When I attempt to db.session.add() a new posts object

Getting first row from sqlalchemy

℡╲_俬逩灬. 提交于 2019-11-28 18:33:30
I have the following query: profiles = session.query(profile.name).filter(and_(profile.email == email, profile.password == password_hash)) How do I check if there is a row and how do I just return the first (should only be one if there is a match)? Lukas Graf Use query.one() to get one, and exactly one result. In all other cases it will raise an exception you can handle: from sqlalchemy.orm.exc import NoResultFound from sqlalchemy.orm.exc import MultipleResultsFound try: user = session.query(User).one() except MultipleResultsFound, e: print e # Deal with it except NoResultFound, e: print e #

Flask: How to manage different environment databases?

心已入冬 提交于 2019-11-28 18:02:24
I am working on an app which looks similar to facebook/ __init__.py feed/ __init__.py business.py views.py models/ persistence.py user.py chat/ __init__.py models.py business.py views.py config/ dev.py test.py prod.py I want to have three environments Dev , Test and Production . I have the following requirements: a.) When I start the server python runserver.py , I would like to mention which environment I want to connect - Dev , Test or Production . b.) Dev & Production should have the schema built and just need to connect to machine c.) I would also like for my test to connect to sqlite db ,

unable to create autoincrementing primary key with flask-sqlalchemy

老子叫甜甜 提交于 2019-11-28 17:50:42
I want my model's primary key to be an autoincrementing integer. Here is how my model looks like class Region(db.Model): __tablename__ = 'regions' id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(100)) parent_id = db.Column(db.Integer, db.ForeignKey('regions.id')) parent = db.relationship('Region', remote_side=id, primaryjoin=('Region.parent_id==Region.id'), backref='sub-regions') created_at = db.Column(db.DateTime, default=db.func.now()) deleted_at = db.Column(db.DateTime) The above code creates my table but does not make id autoincrementing. So if

Implementing Flask-Login with multiple User Classes

旧城冷巷雨未停 提交于 2019-11-28 17:37:43
I am writing an app that has multiple classes that function as Users (for example, a School Account and a Staff account). I'm trying to use Flask-Login to make this easy but I'm not quite sure how to make it, so that when a user logs in I can have my app check to see whether or not the username belongs to a School account or Staff account, and then log in appropriately. I know how to figure out which type of account it belongs to (since all usernames must be unique). But after that I'm not sure how to tell the app that I want it to login that specific user. Right now, I only have one universal

Invalid transaction persisting across requests

*爱你&永不变心* 提交于 2019-11-28 17:26:35
问题 Summary One of our threads in production hit an error and is now producing InvalidRequestError: This session is in 'prepared' state; no further SQL can be emitted within this transaction. errors, on every request with a query that it serves, for the rest of its life! It's been doing this for days , now! How is this possible, and how can we prevent it going forward? Background We are using a Flask app on uWSGI (4 processes, 2 threads), with Flask-SQLAlchemy providing us DB connections to SQL

read slave , read-write master setup

99封情书 提交于 2019-11-28 16:53:46
I have a Flask,SQLAlchemy webapp which uses a single mysql server. I want to expand the database setup to have a read-only slave server such that I can spread the reads between both master and slave while continuing to write to the master db server. I have looked at few of options and I believe I can't do this with plain SQLAlchemy. Instead Im planning to create 2 database handles in my webapp, one each for master and slave db servers. Then using a simple random value use either the master/slave db handle for "SELECT" operations. However Im not sure if this is the right way to go with using