I am unable to retrive data from a second database using flask. Here is my code.
from datetime import datetime, timedelta, timezone
from flask import Flask, redirect, render_template, request, url_for
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["DEBUG"] = True
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
username="SeiunProgram",
password="XXXXXX",
hostname="SeiunProgram.mysql.pythonanywhere-services.com",
databasename="SeiunProgram$comments"
)
SQLALCHEMY_BINDS = {
"nisi" : "mysql+mysqlconnector://SeiunProgram:XXXXX@SeiunProgram.mysql.pythonanywhere-services.com/SeiunProgram$nisi"
}
app.config["SQLALCHEMY_DATABASE_URI"] = SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_BINDS"] = SQLALCHEMY_BINDS
app.config["SQLALCHEMY_POOL_RECYCLE"] = 299
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
db = SQLAlchemy(app)
class Comment(db.Model):
__tablename__ = "comments"
id = db.Column(db.Integer, primary_key=True)
day = db.Column(db.String(20))
content = db.Column(db.String(4096))
log = db.Column(db.String(20))
class Nisi(db.Model):
__bind_key__ = "nisi"
id = db.Column(db.Integer, primary_key=True)
day = db.Column(db.String(20))
content = db.Column(db.String(4096))
print(Nisi.query.all())
print(Nisi.query.id)
I followed the steps in A beginner's guide to building a simple database-backed Flask website on PythonAnywhere, so there is only this python code. I ran this code but the "print(Nisi.query.all)" returns [None]. However the database Nisi properly has queries which contain data.
I checked error log. It said "AttributeError: 'BaseQuery' object has no attribute 'id'" This is the structure of Nisi.
I didnt know how to insert a picture
What shoud I do to get the data of Nisi, specifically in regards to setting binds?
[edit by admin: formatting, removed passwords]