1

I have an issue with Flask. Here's my code. I got the error
AttributeError: 'function' object has no attribute 'name'

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:1351996@localhost:5432/example'

db = SQLAlchemy(app)

class Person(db.Model):
    __tablename__ = 'persons'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(), nullable=False)

db.create_all()

@app.route('/')
def index():
    person = Person.query.first
    return 'Hello ' + person.name
    
if __name__ == '__main__':
    app.run(debug=True)

I used app.run(debug=True) because I got another error when trying to run app using FLASK_APP=app.py, flask run and I don't know why this error appears to me!

1 Answer 1

2

you're not actually executing the query. your index function should be

@app.route('/')
def index():
    person = Person.query.first()
    return 'Hello ' + person.name

Note the parentheses after the .first

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.