flask-sqlalchemy

Flask-Sqlalchemy Acess different schema?

柔情痞子 提交于 2019-12-06 16:48:06
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? Assuming there's something in the config along the lines of PRODUCTION = False/True, then you should be able

Why is “Object of type Decimal is not JSON serializable” - when using marshmallow with SQLAlchemy automap?

泪湿孤枕 提交于 2019-12-06 16:43:38
Using automap_base from sqlalchemy.ext.automap to map my tables. Not able to shema.dumps(result) ; getting raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Decimal is not JSON serializable Tried using JSON custom decoders, but no use. from flask import Flask from flask_sqlalchemy import SQLAlchemy from sqlalchemy.orm import Session from sqlalchemy.ext.automap import automap_base from flask_marshmallow import Marshmallow app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' db = SQLAlchemy(app) ma = Marshmallow(app) engine = db

SqlAlchemy changes in Model description not applied in DB

爱⌒轻易说出口 提交于 2019-12-06 15:36:50
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 models during development? I hardly think manual MySql manipulation is the best solution. you can proceed

Flask-Sqlalchemy .in_() method not working for me

半世苍凉 提交于 2019-12-06 13:57:30
问题 I'm currently working on a project with Flask and SQLAlchemy, basically forum software like discourse but in Python 3. There are Two models at the moment, Forum and Threads I've got a Jinja template that should be generating table records of forum threads, please see below. {% for thread in Forum.ForumThreads.filter_by(TagID.in_(TagsToShow)): %} <TR Class="ThreadRecord"> <TD><a href="{{thread.ForumID}}/Thread/{{thread.ThreadID}}">{{thread.Title}}</a></TD> <TD class="ThreadTag{{thread.TagID}}"

Updating multiple columns in Sqlalchemy

社会主义新天地 提交于 2019-12-06 13:35:43
I have an app that runs on flask and uses sqlalchemy to interact with the data base. I want to update the columns of a table with the user specified values. The query that I am using is def update_table(value1, value2, value3): query = update(Table).where(Table.column1 == value1).values(Table.column2 = value2, Table.column3 = value3) I am not sure if the way I am passing values is correct. Also the Table.column2/3 i=gives error saying Can't assign to function call . Where column2/3 are not functions, they're field names. So, how do I update multiple values and why is it giving error here? PS:

How to get Flask-SQLAlchemy to work with the Application Factory Pattern

北城以北 提交于 2019-12-06 11:52:12
问题 I want to set up an sqlite database using Flask-SQLAlchemy. I am getting an Operational error (sqlite3.OperationalError) no such table . This is for a Web app written with flask. I want to interact with the database using Flask-SQLAlchemy extension. I feel it may be something to do with the application context, but I am not sure. As you can see the application has one 'module' (the auth sub-package). The module is registered to the application via a blueprint. I define the model for the

Many-to-Many with three tables relating with each other (SqlAlchemy)

爷,独闯天下 提交于 2019-12-06 11:32:43
I've three tables User, Device and Role. I have created a many-to-many relation b/w User and Device like this; #Many-to-Many relation between User and Devices userDevices = db.Table("user_devices", db.Column("id", db.Integer, primary_key=True), db.Column("user_id", db.Integer, db.ForeignKey("user.id")), db.Column("device_id", db.Integer, db.ForeignKey("device.id")))) class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(60), index=True, unique=True) devices = db.relationship("Device", secondary=userDevices, backref=db.backref(

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

旧街凉风 提交于 2019-12-06 10:42:29
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 %} <td> {{ inv.number }} </td> </tr> <tr> <td> <div id="accordion" class="collapse"> {{ inv.data }} </div>

How do I properly set up flask-admin views with using an application factory?

夙愿已清 提交于 2019-12-06 10:25:56
问题 I'm trying to setup flask-admin model views with SQLAlchemy against 'user' and 'role' models. Instead of a function admin view I'm getting: ValueError: Invalid model property name <class 'app.models.Role'>.desc Stack trace: Traceback (most recent call last): File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/manage.py", line 18, in <module> app = create_app(os.getenv('APP_CONFIG') or 'default') File "/Users/dbg/Projects/Python/Current/ziff/flaskbase/app/__init__.py", line 49, in create

SQLAlchemy: filter many-to-one relationship where the one object has a list containing a specific value

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-06 08:03:57
问题 I have some tables like this: class Genre(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), index=True) artist_id = db.Column(db.Integer, db.ForeignKey('artist.id')) class Song(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128), index=True) artist = db.relationship('Artist', uselist=False) artist_id = db.Column(db.Integer, db.ForeignKey('artist.id')) class Artist(db.Model): id = db.Column(db.Integer, primary_key=True)