I have created a table with SQLAlchemy inside a flask application in this application I implemented a python code to query data in an SQLite database.
def create_connection(db_file):
conn = None
try:
conn = sqlite3.connect(db_file)
except Error as e:
print(e)
return conn
def select_itens(conn, column):
cur = conn.cursor()
cur.execute(" SELECT %s FROM table WHERE id=1" %(column))
rows = cur.fetchall()
return rows
def data_select(column):
# create a database connection
conn = create_connection(database)
with conn:
data = select_itens(conn, column)
return data
On my local host the project works perfectly. However, when I deployed it to heroku I caught some errors.
File "/app/.heroku/python/lib/python3.9/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/app/main.py", line 124, in services
data_select("user")
File "/app/database.py", line 76, in data_select
select_itens(conn, column)
File "/app/database.py", line 51, in select_itens
cur.execute(" SELECT %s FROM teste WHERE id=1" %(column))
sqlite3.OperationalError: no such table: teste
I migrated my database following this post Setting up flask app in heroku with a database. But from the error it says that the table does not exist, so possibly I am forgetting something.
This test table was created via the command prompt as follows:
->> from main import db
->> db.create_all()
main.py
db = SQLAlchemy(app)
#Create db model
class Teste(db.Model):
id = db.Column(db.Integer, primary_key = True)
user = db.Column(db.String(50), nullable = False)
date = db.Column(db.DateTime, default=datetime.utcnow)
#Create function to return a srting when we add something
def __repr__(self):
return '<Information %r>' % self.id
This is the first application I have deployed with a database, so I am still learning. Do I need to create the test table inside heroku or does the migration I did solve this problem?