flask-sqlalchemy

Flask-Json How do i return mutiple rows as json objects

筅森魡賤 提交于 2019-12-25 18:12:33
问题 I have a users column and i am trying to output all of its data on an html file via json and ajax. i am able to return multiple columns from a single row. But i am unable to figure out how to return and call multiple rows. Here is my code and also a couple of commented thing i have tried. @app.route('/test', methods=['POST', 'GET']) def test(): #thisworks works = user.query.filter_by(name = 'foo').first() return jsonify(name = works.name, id = works.id) #BELOW LINES DONT WORK #st = user.query

Flask-SQLAlchemy Single Table Inheritance

时光总嘲笑我的痴心妄想 提交于 2019-12-25 09:39:13
问题 SQLAlchemy support single table inheritance. I have structure like: class User(db.Model): __tablename__ = 'tbl_user' type = db.Column(db.String(32)) ... __mapper_args__ = { 'polymorphic_identity': 'user', 'polymorphic_on': type, 'with_polymorphic': '*' } class Tourist(User): __mapper_args__ = { 'polymorphic_identity': 'tourist' } ... class Guide(User): __mapper_args__ = { 'polymorphic_identity': 'guide' } ... When I try to run code, i get error like: sqlalchemy.exc.InvalidRequestError: Table

Creating Table from dictionary in SQLAlchemy

空扰寡人 提交于 2019-12-25 09:34:57
问题 I'm trying to create a table from dictionary values in SQLAlchemy. I'm using Flask, and currently my class looks like this: class Machine(db.Model): """Template for the Machine Info table""" __tablename__ = 'machine' id = db.Column(db.Integer, primary_key=True) machine_name = db.Column(db.String(32)) date = db.Column(db.String(32)) time = db.Column(db.String(32)) sensor1 = db.Column(db.String(32)) sensor2 = db.Column(db.String(32)) This works fine, but my issue is that I will eventually have

Vague Flask-SQLAlchemy error: “AttributeError: mapper”

老子叫甜甜 提交于 2019-12-25 09:25:00
问题 Okay, so I have what seems to be a pretty basic model definition. I don't have the first clue where to begin with this. The only thing I could really think to check is that the table exists, and it does, and the db.create_all() function seems to be able to create the table based on the model definition. The error seems to be so vague that it doesn't really give me any clues as to where to look. What does the error mean basically? That the Setting class is failing to be mapped? That it doesn't

Get data values from QuerySelectField with query_factory

大城市里の小女人 提交于 2019-12-25 08:37:22
问题 I have problem to save data from QuerySelectField with query_factory. I will always end with: Error message InterfaceError: (sqlite3.InterfaceError) Error binding parameter 2 - probably unsupported type. [SQL: u'INSERT INTO asset_objects (asset_name, asset_type, asset_owner) VALUES (?, ?, ?)'] [parameters: (u'd', u'Process', <__main__.Model_New_Users object at 0x03819A50>)] DB Model # --- New Information Assets --- class Model_New_Asset(db.Model): __tablename__ = 'asset_objects' id = db

How to use sum and count in a joined query on sqlAlchemy models

徘徊边缘 提交于 2019-12-25 08:29:02
问题 I have working SQL that I need to represent in a working Flask app that is using SQLAlchemy as an ORM. I can represent the joins, etc., but am getting nowhere with tying in the sums, counts, and the other specific select element, u.firstname || ' ' || u.lastname . SQL select u.firstname || ' ' || u.lastname SELLER, count(i.id) UNIQUE_ITEMS, sum(i.qty) ITEMS_TOTAL, sum(i.qty * p.price) ORDER_AMOUNT from orders o INNER JOIN order_items i on o.id = i.order_id and o.campaign_id = 133 INNER JOIN

How can sqlalchemy work with custom class as the attributes

时光怂恿深爱的人放手 提交于 2019-12-25 07:59:52
问题 I am using sqlalchemy and flask-sqlalchemy, one of my db model comes with a state column class Item(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(8)) state = db.Column(db.Integer, default=ServerState.IN_PACKAGE, nullable=True) def init_state(self): self.state = State1 def transit_state(self): self.state.next(self) The problem is that this state column is saved into db as a integer, but in order to implement a proper state machine, it is implemented as a

How to get a SQL Alchemy object of distinct items with filter on related objects

别等时光非礼了梦想. 提交于 2019-12-25 07:46:45
问题 I am trying to get a SQL Alchemy query of distinct items below filtering on related objects, the equivalent of the below query: SELECT distinct items.item_id, items.item_name FROM items INNER JOIN categories as cat on items.category_id = cat.category_id INNER JOIN stores on cat.store_id = stores.store_id WHERE store.store_id = 123 I have created the models as below with foreign keys included but when I run the query below it does not filter correctly. items_query = (db.session.query(Store,

I have psycopg2 installed in virtual env, but still getting importerror psycopg2

点点圈 提交于 2019-12-25 06:47:32
问题 I followed the following steps: [I am using Windows 8, Python 2.7.6] I created a virtual environment venv and then did pip install on requirements.txt to install all the libraries. I had to explicitly create a file called config.py to set the os.environ variables. Then, I am doing import config in other files. I have installed postgresql and added bin folder to environment path psycopg2 was failing to install from running requirements.txt. So, I downloaded the zip with setup.py from online,

Flask App returning “MySQL server has gone away” after 60 seconds of idle time

ε祈祈猫儿з 提交于 2019-12-25 06:29:59
问题 I'm using Flask-SQLAlchemy in my app and after 60 seconds of idle time, MySQL returns a "MySQL server has gone away" error. Here's my traceback: Traceback (most recent call last): File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/flask/app.py", line 1687, in wsgi_app response = self.full_dispatch_request() File "/var/www/vhosts/example.com/httpdocs/venv/lib/python2.7/site-packages/flask/app.py", line 1360, in full_dispatch_request rv = self.handle_user_exception(e)