flask-sqlalchemy

Flask and SQLAlchemy, application not registered on instance

折月煮酒 提交于 2019-12-18 13:34:28
问题 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

SQLAlchemy - Querying with DateTime columns to filter by month/day/year

北慕城南 提交于 2019-12-18 13:25:15
问题 I'm building a Flask website that involves keeping track of payments, and I've run into an issue where I can't really seem to filter one of my db models by date. For instance, if this is what my table looks like: payment_to, amount, due_date (a DateTime object) company A, 3000, 7-20-2018 comapny B, 3000, 7-21-2018 company C, 3000, 8-20-2018 and I want to filter it so that I get all rows that's after July 20th, or all rows that are in August, etc. I can think of a crude, brute-force way to

How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

╄→гoц情女王★ 提交于 2019-12-18 09:59:54
问题 Every time I run my app that uses Flask-SQLAlchemy I get the following warning that the SQLALCHEMY_TRACK_MODIFICATIONS option will be disabled. /home/david/.virtualenvs/flask-sqlalchemy/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning. warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be

flask admin : sqlalchemy.exc.InterfaceError(Error binding parameter 8)

时光怂恿深爱的人放手 提交于 2019-12-18 09:51:18
问题 I was trying to save a project's reviewer using below, and the select field shows correct: # Query the user with Role.id == 4 as reviewer def reviewer_choices(): return User.query.join(User.roles).filter(Role.id == 4) # Build a select field class ProjectView(sqla.ModelView): form_extra_fields = { 'reviewer': sqla.fields.QuerySelectField( label='Reviewer', query_factory=reviewer_choices, )} However, when I was trying to save it, the error occurred: InterfaceError: (sqlite3.InterfaceError)

How to call stored procedure with SQLAlchemy that requires a user-defined-type Table parameter

坚强是说给别人听的谎言 提交于 2019-12-18 09:03:53
问题 I have a stored procedure on MSSQL server, "prc_add_names", that takes a table-value parameter. The parameter itself is of a custom type "StringTable" defined like so: CREATE TYPE [dbo].[StringTable] AS TABLE([strValue] [nvarchar](max) NULL) I have no idea how to execute this procedure using SQLAlchemy. I am used to calling procedures with arguments using session.execute like this: result = session.execute('prc_do_something :pArg', {pArg:'foo'}) However, this does not work if I simply pass a

SQLAlchemy Return All Distinct Column Values

假装没事ソ 提交于 2019-12-18 04:15:20
问题 I am creating a website using Flask and SQLAlchemy. This website keeps track of classes that a student has taken. I would like to find a way to search my database using SQLAlchemy to find all unique classes that have been entered. Here is code from my models.py for Class: class Class(db.Model): __tablename__ = 'classes' id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100)) body = db.Column(db.Text) created = db.Column(db.DateTime, default=datetime.datetime.now) user

Updates to JSON field don't persist to DB

拥有回忆 提交于 2019-12-18 03:58:06
问题 We have a model with a JSON field where user flags get inserted. Inserting does work as expected, but when removing certain flags, they stay in the field and changes don't get persisted to the DB. We have the following method in our model: def del_flag(self, key): if self.user_flags is None or not key in self.user_flags: return False else: del self.user_flags[key] db.session.commit() return True The databasse is postgres and we use the SQLalchemy JSON field dialect for the field type. Any

Connect to MSSQL Database using Flask-SQLAlchemy

有些话、适合烂在心里 提交于 2019-12-18 01:15:13
问题 I'm trying to connect to a local MSSQL DB through Flask-SQLAlchemy. Here's a code excerpt from my __init__.py file: from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://HARRISONS-THINK/LendApp' db = SQLAlchemy(app) SQLALCHEMY_TRACK_MODIFICATIONS = False As you can see in SQL Server Management Studio, this information seems to match: Here is the creation of a simple table in my models.py file: from LendApp

How to use flask-sqlalchemy with existing sqlalchemy model?

吃可爱长大的小学妹 提交于 2019-12-17 22:54:23
问题 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

Flask: How to manage different environment databases?

北城以北 提交于 2019-12-17 22:34:44
问题 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