flask-sqlalchemy

Multiple Identical Tables in Flask-SQLAlchemy

纵饮孤独 提交于 2019-12-23 06:04:43
问题 I'm creating a REST-like API to get data in and out of a legacy database. At the moment I don't have the option to alter the db structure. Nearly every table in the database has the exact same structure. Here's how I'm currently handling it using Flask-SQLAlchemy: class MyDatasetBase(object): timestamp = db.Column('colname', db.Float, primary_key=True) val1 = db.Column(db.Float) val2 = db.Column(db.Float) val3 = db.Column(db.Float) fkeyid = db.Column(db.Integer) @declared_attr def

SqlAlchemy changes in Model description not applied in DB

泪湿孤枕 提交于 2019-12-23 03:07:35
问题 I am currently developing a server using Flask/SqlAlchemy. It occurs that when an ORM model is not present as a table in the database, it is created by default by SqlAlchemy. However when an ORM class is changed with for instance an extra column is added, these changes do not get saved in the database. So the extra column will be missing, every time I query. I have to adjust my DB manually every time there is a change in the models that I use. Is there a better way to apply changes in the

Returning custom property using with_entities() in flask-sqlalchemy?

我只是一个虾纸丫 提交于 2019-12-23 03:07:02
问题 I have a custom property on my model. I'm using with_entities() to restrict what is returned on my model. For example, class User(Model): id = Column(Integer, primary_key=True) first_name = Column(String) last_name = Column(String) @property def slug(self): return slugify(self.first_name + ' ' + self.last_name) How can I return my model that only has first_name and slug ? This throws an error: # Try with User.slug user = User.query.with_entities(User.first_name, User.slug).filter_by(id=1)

Flask-Sqlalchemy Acess different schema?

走远了吗. 提交于 2019-12-23 01:07:13
问题 Say I have a Postgres database with two different schema's, one called public and one called development . I know at the class level I can set __table_args__ = {"schema":"schema_name"} for each model, but if I wanted to switch from development to prod. wouldn't I haven't to update all my models? Is there a way to set the schema for the dev and prod schemas in flask? Or should I just create another database as a dev database and just backup the prod database to the development database? 回答1:

Flask SQL-Alchemy | MySql - Multiple Foreign Keys issues

こ雲淡風輕ζ 提交于 2019-12-22 19:49:43
问题 class Role(db.Model): __tablename__ = 'roles' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(64), unique=True) default = db.Column(db.Boolean, default=False, index=True) permissions = db.Column(db.Integer) class Devices(db.Model): __tablename__ = 'devices' id = db.Column(db.Integer, primary_key=True) purpose = db.Column('purpose', db.String(64)) type = db.Column('type', db.String(64)) name = db.Column('name', db.String(64)) channel = db.Column('channel', db.Integer)

Create Bootstrap collapsible accordion table rows that display dynamic content from flask sqlalchemy placeholders

爷,独闯天下 提交于 2019-12-22 18:34:09
问题 Consider the following table which iterates data from an sqlite database using flask and sqlalchemy . Assume for this example that the data is a list of invoices and clicking on each row opens a collapsible bootstrap accordion whith further information for the clicked invoice. <table class="table table-hover" data-toggle="table"> <thead> <tr> <th>Date</th> <th>Invoice</th> </tr> </thead> <tbody> <tr data-toggle="collapse" data-target="#accordion" class="clickable"> {% for inv in invoices %}

Sort by Count of Many to Many Relationship - SQLAlchemy

穿精又带淫゛_ 提交于 2019-12-22 11:44:46
问题 I am using Flask-SQLAlchemy to to query my Postgres database. I am currently trying to query for suggestions of titles with the following query: res = Title.query.filter(Titles.name.ilike(searchstring)).limit(20) So far so good. Now I would like to order the results by the number of "subscribers" each Title object has. I am aware of the following SO question: SQLAlchemy ordering by count on a many to many relationship however its solution did not work for me. I am receiving the following

Batch Editing in Flask-Admin

为君一笑 提交于 2019-12-22 10:06:32
问题 I'm using Flask-Admin and I want to be able to update many fields at once from the list view. It seemed like what I'm looking for is a custom action. I was able to make it work, but I suspect not in the best way. I'm wondering if it could be done more "Flask"-ily. What I do now, for example if I was updating all rows in table cars to have tires = 4 : A custom action in the CarView class collects the ids of the rows to be modified, a callback url from request.referrer , and the tablename cars

Pass encoding parameter to cx_oracle from sqlalchemy

时光怂恿深爱的人放手 提交于 2019-12-22 08:59:30
问题 I'm using Oracle Database with UTF-16 encoding. The diacritics is correctly displayed when using directly cx_oracle client. Connection statement is like this: cx_Oracle.connect(username, password, conn_str, encoding='UTF-16', nencoding='UTF-16') However, now I'm building bigger application and I would like to use SQLalchemy in Flask . Code looks like this: from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy(app) db.Model.metadata.reflect(db.engine) class MyTable(db.Model): __table__ = db

Table(Model) Inheritance with Flask SQLAlchemy

試著忘記壹切 提交于 2019-12-22 05:50:11
问题 I followed the advice in this question but I'm still receiving this error: sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'investment' and 'real_estate'. Models class Investment(db.Model): __tablename__ = 'investment' __mapper_args__ = {'polymorphic_identity': 'investment'} id = Column(db.Integer, primary_key=True) item_name = Column(db.String(64)) investment_type = Column(db.String(32), nullable=False) __mapper_args__ = {'polymorphic_on': investment_type}