flask-sqlalchemy

sqlalchemy foreign key relationship attributes

十年热恋 提交于 2019-11-28 16:17:13
问题 I have a User table and a Friend table. The Friend table holds two foreign keys both to my User table as well as a status field. I am trying to be able to call attributes from my User table on a Friend object. For example, I would love to be able to do something like, friend.name, or friend.email. class User(Base): """ Holds user info """ __tablename__ = 'user' id = Column(Integer, primary_key=True) name = Column(String(25), unique=True) email = Column(String(50), unique=True) password =

Share sqlalchemy models between flask and other apps

妖精的绣舞 提交于 2019-11-28 16:14:33
I have a running Flask application that is set up according to a combination of best practices we found online and in Miguel Grinberg's "Flask Web Development" book. We now need a second Python application, that is NOT a web app, and that needs access to the same models as the Flask application. We wanted to re-use the same models ofcourse, so both apps can benefit from the shared code. We have removed dependencies on the flask-sqlalchemy extention (which we used before, when we had just the Flask application). And replaced it with the SQLalchemy Declarative extension described here , which is

Creating seed data in a flask-migrate or alembic migration

我的未来我决定 提交于 2019-11-28 15:49:24
问题 How can I insert some seed data in my first migration? If the migration is not the best place for this, then what is the best practice? """empty message Revision ID: 384cfaaaa0be Revises: None Create Date: 2013-10-11 16:36:34.696069 """ # revision identifiers, used by Alembic. revision = '384cfaaaa0be' down_revision = None from alembic import op import sqlalchemy as sa def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.create_table('list_type', sa.Column('id', sa

How do I produce nested JSON from database query with joins? Using Python / SQLAlchemy

≯℡__Kan透↙ 提交于 2019-11-28 14:36:54
I have a specify use case but my question pertains to the best way of doing this in general. I have three tables Order - primary key order_id OrderLine - Linking table with order_id, product_id and quantity. An order has 1 or more order lines Product - primary key product_id, each order line has one product In sqlachemy / python how do I generate nested JSON along the lines of: { "orders": [ { "order_id": 1 "some_order_level_detail": "Kansas" "order_lines": [ { "product_id": 1, "product_name": "Clawhammer", "quantity": 5 }, ... ] }, ... ] } Potential Ideas Hack away doing successive queries

sqlalchemy - reflecting tables and columns with spaces

白昼怎懂夜的黑 提交于 2019-11-28 14:00:39
How can I use sqlalchemy on a database where the column names (and table names) have spaces in them? db.auth_stuff.filter("db.auth_stuff.first name"=='Joe') obviously can't work. Rather than manually define everything when doing the reflections I want to put something like lambda x: x.replace(' ','_') between existing table names being read from the db, and being used in my models. (It might also be useful to create a general function to rename all table names that won't work well with python - reserved words etc.) Is there an easy/clean way of doing this? I think I need to define my own

How to filter exact many to many

旧巷老猫 提交于 2019-11-28 12:56:14
问题 I have User and Room model in Flask SQLAlchemy. I need to filter if Room exists with users [user1, user2, ...]. Filter must be exact. Here are my models: room_users_table = db.Table( 'room_users', db.metadata, db.Column('user', db.Integer, db.ForeignKey('user.id')), db.Column('room', db.Integer, db.ForeignKey('room.id')) ) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) first_name = db.Column(db.String(80)) last_name = db.Column(db.String(80))

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

你说的曾经没有我的故事 提交于 2019-11-28 12:38:47
问题 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):

Join tables in two databases using SQLAlchemy

為{幸葍}努か 提交于 2019-11-28 11:38:07
I am working with two MySQL Databases. I want to join a table from DB 1 with a table from DB2 in SQLAlchemy. I am using automap_base while creating data access layer in sqlalchemy as follows... class DBHandleBase(object): def __init__(self, connection_string='mysql+pymysql://root:xxxxxxx@localhost/services', pool_recycle=3600): self.Base_ = automap_base() self.engine_ = create_engine(connection_string, pool_recycle = pool_recycle) self.Base_.prepare(self.engine_, reflect=True) self.session_ = Session(self.engine_) And my tables Class is like class T1D1_Repo(): def __init__(self, dbHandle): #

flask-admin form: Constrain Value of Field 2 depending on Value of Field 1

拥有回忆 提交于 2019-11-28 08:35:01
One feature I have been struggling to implement in flask-admin is when the user edits a form, to constrain the value of Field 2 once Field 1 has been set. Let me give a simplified example in words (the actual use case is more convoluted). Then I will show a full gist that implements that example, minus the "constrain" feature. Let's say we have a database that tracks some software "recipes" to output reports in various formats. The recipe table of our sample database has two recipes: "Serious Report", "ASCII Art". To implement each recipe, we choose one among several methods. The method table

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

大憨熊 提交于 2019-11-28 08:02:16
问题 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? 回答1: 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