4

I'm new using flask and SQLAlchemy, I have a database in postgress, this database have table "data" 200 records, I want to do a Select statment but when I do it always give me the same error, this my code:

This is my model.py

from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Data(Base):
    __tablename__ = 'data'
    ids = Column(Integer(), primary_key=True)
    customer_id = Column(String())
    inventory_id = Column(String())
    device_id = Column(String())

    def toJSON(self):       
        json = {
            "ids":self.ids,
            "customer_id":self.customer_id,
            "inventory_id":self.inventory_id,
            "device_id":self.device_id,     
        }

        return json

This is my flask application:

from flask_sqlalchemy import SQLAlchemy
from flask import Flask, render_template, request
from flask_cors import CORS, cross_origin
from model import Data, Base

app = Flask(__name__)
CORS(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost/mydatabase'
db = SQLAlchemy(app)
data = []

def index():
    test_data()
    return render_template("index.html",result = data)

def test_data():
    sss = Base.Data.query.filter_by(ids=5).first()
    print sss

app.add_url_rule('/', 'index', index)

if __name__ == '__main__':
    app.run(debug = True, threaded=True)

This not works, and always have the same error:

AttributeError: type object 'Base' has no attribute 'psirt_alerts_view'

How can I do this simply select?

UPDATE:

[2017-02-14 12:02:29,980] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "c:\python27\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "c:\python27\lib\site-packages\flask_cors\extension.py", line 161, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
  File "c:\python27\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "c:\python27\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "c:\python27\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "S:\ibm\ib.py", line 15, in index
    test_data()
  File "S:\ibm\ib.py", line 40, in test_data
    sss = Base.data.query.filter_by(ids=5).first()
AttributeError: type object 'Base' has no attribute 'data'
12
  • 2
    Please add the full error traceback to your question! Commented Feb 14, 2017 at 11:37
  • If you can create your Data model using flask_sqlalchemy you can do Data.query. Commented Feb 14, 2017 at 11:39
  • Data should be in uppercase not Base.data, it's your class name Commented Feb 14, 2017 at 11:41
  • @Arman not works with uppercase Commented Feb 14, 2017 at 11:46
  • If you have imported Data directly why using Base.data ? Commented Feb 14, 2017 at 11:47

1 Answer 1

7

db instance has session object so you can do query like this:

db.session.query(Data).filter_by(ids=5).first()
Sign up to request clarification or add additional context in comments.

3 Comments

Nice to hear that ;)
where does this line of code go in the actual flask app? in the models or view code ?
It’s a query you can use it anywhere you like.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.