MySQL Connection not available. Database PythonAnywhere for Telegram Bot, SQLAlchemy - python

The problem is this: I run the script, everything works fine. a few minutes pass and the script shuts down, leaving this error.
here is the script:
import telebot
from sqlalchemy import create_engine, Column, Integer, BigInteger
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
telegram_id = Column(BigInteger, unique=True)
money = Column(Integer)
money_for_investing = Column(Integer)
invited_friends = Column(Integer)
# Connect to the database
engine = create_engine('mysql+mysqlcon...', pool_recycle=3600)
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
# Initialize the bot and set the access token
bot = telebot.TeleBot("BOT TOKEN")
# Define the /start command handler
#bot.message_handler(commands=["start"])
def start(message):
# Get the user's input
cash = 0
user_id = message.from_user.id
investing_cash = 0
invited_friends = 0
user = session.query(User).filter(User.telegram_id == user_id).first()
# If the user is not in the database, store the data
if not user:
user = User(telegram_id=user_id, money=cash, money_for_investing=investing_cash, invited_friends=invited_friends)
session.add(user)
session.commit()
# Send a confirmation message
chat_id = message.chat.id
bot.send_message(chat_id=chat_id, text="You are registered.")
else:
# Send an error message
chat_id = message.chat.id
bot.send_message(chat_id=chat_id, text="You are already registered.")
# Start the bot
bot.polling(none_stop=True, interval=0)
Error:
20:15 ~/telegram $ python telegram_bot.py
Traceback (most recent call last):
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1719, in _execute_context
context = constructor(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 1013, in _init_compiled
self.cursor = self.create_cursor()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 1361, in create_cursor
return self.create_default_cursor()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 1364, in create_default_cursor
return self._dbapi_connection.cursor()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 1083, in cursor
return self.dbapi_connection.cursor(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 590, in cursor
raise errors.OperationalError("MySQL Connection not available.")
mysql.connector.errors.OperationalError: MySQL Connection not available.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/alexknowsbest/telegram/telegram_bot.py", line 51, in <module>
bot.polling(none_stop=True, interval=0)
File "/home/alexknowsbest/.local/lib/python3.10/site-packages/telebot/__init__.py", line 1043, in polling
self.__threaded_polling(non_stop=non_stop, interval=interval, timeout=timeout, long_polling_timeout=long_polling_timeout,
File "/home/alexknowsbest/.local/lib/python3.10/site-packages/telebot/__init__.py", line 1118, in __threaded_polling
raise e
File "/home/alexknowsbest/.local/lib/python3.10/site-packages/telebot/__init__.py", line 1074, in __threaded_polling
self.worker_pool.raise_exceptions()
File "/home/alexknowsbest/.local/lib/python3.10/site-packages/telebot/util.py", line 156, in raise_exceptions
raise self.exception_info
File "/home/alexknowsbest/.local/lib/python3.10/site-packages/telebot/util.py", line 100, in run
task(*args, **kwargs)
File "/home/alexknowsbest/.local/lib/python3.10/site-packages/telebot/__init__.py", line 6395, in _run_middlewares_and_handler
result = handler['function'](message)
File "/home/alexknowsbest/telegram/telegram_bot.py", line 34, in start
user = session.query(User).filter(User.telegram_id == user_id).first()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2819, in first
return self.limit(1)._iter().first()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/query.py", line 2903, in _iter
result = self.session.execute(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/orm/session.py", line 1696, in execute
result = conn._execute_20(statement, params or {}, execution_options)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1631, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/sql/elements.py", line 325, in _execute_on_connection
return connection._execute_clauseelement(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1498, in _execute_clauseelement
ret = self._execute_context(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1725, in _execute_context
self._handle_dbapi_exception(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 2043, in _handle_dbapi_exception
util.raise_(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/base.py", line 1719, in _execute_context
context = constructor(
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 1013, in _init_compiled
self.cursor = self.create_cursor()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 1361, in create_cursor
return self.create_default_cursor()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/engine/default.py", line 1364, in create_default_cursor
return self._dbapi_connection.cursor()
File "/usr/local/lib/python3.10/site-packages/sqlalchemy/pool/base.py", line 1083, in cursor
return self.dbapi_connection.cursor(*args, **kwargs)
File "/usr/local/lib/python3.10/site-packages/mysql/connector/connection_cext.py", line 590, in cursor
raise errors.OperationalError("MySQL Connection not available.")
sqlalchemy.exc.OperationalError: (mysql.connector.errors.OperationalError) MySQL Connection not available.
[SQL: SELECT users.id AS users_id, users.telegram_id AS users_telegram_id, users.money AS users_money, users.money_for_investing AS users_money_for_investing, users.invited_friends AS users_invited_friends
FROM users
WHERE users.telegram_id = %(telegram_id_1)s
LIMIT %(param_1)s]
[parameters: [{}]]
(Background on this error at: https://sqlalche.me/e/14/e3q8)
I've been trying to fix this mistake for a day, I can't. I switch to different modules and still nothing happens. please help me solve this problem. I will be very grateful to you

Related

Flask SQLAlquemy db.commit breaks stuff

I am learning about Rest APIs and Flask. Following a simple tutorial, something doesn't work and I can't get it fixed. I create a database and I want to add something to it. My file is called "API.py" and I am using PyCharm. After running the file, I type into the Python Console in Python:
from API import db
from API import Drink
Drink.query.all() (returns an empty list, all good)
drink = Drink(name="x", description="y")
Drink.query.all() (returns an empty list, all good)
db.session.add(drink)
Drink.query.all() (returns the drink, all good)
db.session.commit()
Drink.query.all() - ERROR
I tried several things, but I can't figure it out. In the tutorial, everything is working just fine. Would you please help me fix this? Below you will find the error message and the code:
Error message:
Traceback (most recent call last):
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\base.py", line 1800, in _execute_context
context = constructor(
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\default.py", line 1015, in _init_compiled
self.cursor = self.create_cursor()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\default.py", line 1386, in create_cursor
return self.create_default_cursor()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\default.py", line 1389, in create_default_cursor
return self._dbapi_connection.cursor()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\pool\base.py", line 1083, in cursor
return self.dbapi_connection.cursor(*args, **kwargs)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 5244 and this is thread id 9008.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\IPython\core\interactiveshell.py", line 3397, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-7-87e83f2944ed>", line 1, in <cell line: 1>
Drink.query.all()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\orm\query.py", line 2772, in all
return self._iter().all()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\orm\query.py", line 2907, in _iter
result = self.session.execute(
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\orm\session.py", line 1712, in execute
result = conn._execute_20(statement, params or {}, execution_options)
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\base.py", line 1705, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\sql\elements.py", line 333, in _execute_on_connection
return connection._execute_clauseelement(
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\base.py", line 1572, in _execute_clauseelement
ret = self._execute_context(
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\base.py", line 1806, in _execute_context
self._handle_dbapi_exception(
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\base.py", line 2124, in _handle_dbapi_exception
util.raise_(
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\util\compat.py", line 208, in raise_
raise exception
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\base.py", line 1800, in _execute_context
context = constructor(
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\default.py", line 1015, in _init_compiled
self.cursor = self.create_cursor()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\default.py", line 1386, in create_cursor
return self.create_default_cursor()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\engine\default.py", line 1389, in create_default_cursor
return self._dbapi_connection.cursor()
File "C:\Users\larsw\anaconda3\envs\useenv\lib\site-packages\sqlalchemy\pool\base.py", line 1083, in cursor
return self.dbapi_connection.cursor(*args, **kwargs)
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 5244 and this is thread id 9008.
[SQL: SELECT drink.iden AS drink_iden, drink.name AS drink_name, drink.description AS drink_description
FROM drink]
[parameters: [{}]]
(Background on this error at: https://sqlalche.me/e/14/f405)
Code:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
db = SQLAlchemy(app)
class Drink(db.Model):
iden = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
description = db.Column(db.String(120))
def __repr__(self):
return f"{self.name} - {self.description}"
db.create_all()
#app.route('/')
def index():
return "Hey"
#app.route('/drinks')
def get_drinks():
return {"drinks": "drink data"}
if __name__ == "__main__":
app.run(debug=True, port=8000)
Thanks to snakecharmerb's comment, I replaced
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db"
by
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data.db?check_same_thread=False"
and it works just fine now.

Unsure why I get a thread id error when using SQLAlchemy

I'm trying to create a database for a flask market website using models (User and Item).
When I go into my python terminal and execute the following commands:
from market import db
from market.models import User,Item
u1 = User(username='Hay', password_hash = '123', email_address='ahus#gmail.com')
db.session.add(u1)
db.session.commit()
User.query.all()
i1 = Item(name='Iphone X', description='New iphone', barcode='123456789101', price=760)
db.session.add(i1)
db.session.commit()
i2 = Item(name='Macbook', description='New macbook', barcode='123456789102', price=1000)
db.session.add(i2)
db.session.commit()
As soon as I enter db.session.commit() for the second time I get this error:
Traceback (most recent call last):
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1702, in _execute_context
context = constructor(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1013, in _init_compiled
self.cursor = self.create_cursor()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1361, in create_cursor
return self.create_default_cursor()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1364, in create_default_cursor
return self._dbapi_connection.cursor()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\pool\base.py", line 1083, in cursor
return self.dbapi_connection.cursor(*args, **kwargs)
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 24304 and this is thread id 12744.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 3448, in _flush
flush_context.execute()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 456, in execute
rec.execute(self)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 630, in execute
util.preloaded.orm_persistence.save_obj(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 244, in save_obj
_emit_insert_statements(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\persistence.py", line 1221, in _emit_insert_statements
result = connection._execute_20(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1614, in _execute_20
return meth(self, args_10style, kwargs_10style, execution_options)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\sql\elements.py", line 325, in _execute_on_connection
return connection._execute_clauseelement(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1481, in _execute_clauseelement
ret = self._execute_context(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1708, in _execute_context
self._handle_dbapi_exception(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2026, in _handle_dbapi_exception
util.raise_(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 1702, in _execute_context
context = constructor(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1013, in _init_compiled
self.cursor = self.create_cursor()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1361, in create_cursor
return self.create_default_cursor()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 1364, in create_default_cursor
return self._dbapi_connection.cursor()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\pool\base.py", line 1083, in cursor
return self.dbapi_connection.cursor(*args, **kwargs)
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 24304 and this is thread id 12744.
[SQL: INSERT INTO item (name, price, barcode, description, owner) VALUES (?, ?, ?, ?, ?)]
[parameters: [{'barcode': '123456789102', 'price': 760, 'description': 'New laptop', 'name': 'Laptop', 'owner': None}]]
(Background on this error at: https://sqlalche.me/e/14/f405)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 966, in _rollback_impl
self.engine.dialect.do_rollback(self.connection)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 682, in do_rollback
dbapi_connection.rollback()
sqlite3.ProgrammingError: SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 24304 and this is thread id 12744.
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<string>", line 2, in commit
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 1431, in commit
self._transaction.commit(_to_root=self.future)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 829, in commit
self._prepare_impl()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 808, in _prepare_impl
self.session.flush()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 3348, in flush
self._flush(objects)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 3488, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 84, in __exit__
compat.raise_(value, with_traceback=traceback)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 3488, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 900, in rollback
util.raise_(rollback_err[1], with_traceback=rollback_err[2])
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\orm\session.py", line 865, in rollback
t[1].rollback()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2340, in rollback
self._do_rollback()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2544, in _do_rollback
self._close_impl(try_deactivate=True)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2527, in _close_impl
self._connection_rollback_impl()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2519, in _connection_rollback_impl
self.connection._rollback_impl()
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 968, in _rollback_impl
self._handle_dbapi_exception(e, None, None, None, None)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2026, in _handle_dbapi_exception
util.raise_(
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\base.py", line 966, in _rollback_impl
self.engine.dialect.do_rollback(self.connection)
File "C:\Users\simer\PycharmProjects\MarketWebsite_Flask\venv\lib\site-packages\sqlalchemy\engine\default.py", line 682, in do_rollback
dbapi_connection.rollback()
sqlalchemy.exc.ProgrammingError: (sqlite3.ProgrammingError) SQLite objects created in a thread can only be used in that same thread. The object was created in thread id 24304 and this is thread id 12744.
(Background on this error at: https://sqlalche.me/e/14/f405)
I don't know why and how to fix this, does anyone know why this happens and how I can fix it?
This is the code for my models:
from market import db
class User(db.Model):
id = db.Column(db.Integer(), primary_key=True)
username = db.Column(db.String(length=30), nullable=False, unique=True)
email_address = db.Column(db.String(length=50), nullable=False, unique=True)
password_hash = db.Column(db.String(length=60), nullable=False)
budget = db.Column(db.Integer(), nullable=False, default=1000) # Users have 1000 points to spend at the start
items = db.relationship('Item', backref='owned_user', lazy=True)
class Item(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(length=30), nullable=False, unique=True)
price = db.Column(db.Integer(), nullable=False)
barcode = db.Column(db.String(length=12), nullable=False, unique=True)
description = db.Column(db.String(length=1000), nullable=False)
owner = db.Column(db.Integer(), db.ForeignKey('user.id'))
def __repr__(self):
return f'Item {self.name}'
I was executing all those database commands in the PyCharm repl. I believe it has something to do with memory which is why it won't work in the python console on Pycharm. It works fine in a command prompt terminal and in a python file.

flask db init returns "sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table"

I've been working on a web app in flask using an sqlite database. This has been going fine so far, and I am aware of the fact sqlite is limited in terms of alterations to tables once a database exists.
However, so far i have been able to modify tables (add columns, rename columns, etc.) of several models in the models.py without issue. Noting here I do use
migrate = Migrate(app, db, render_as_batch=True)
in the app initialization, which has been needed to realize some changes to the database.
Now however, I have received the following error when trying to run flask db migrate after adding a new column to the model "User":
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/default.py", line 580, in do_execute
cursor.execute(statement, parameters)
sqlite3.OperationalError: no such table: user
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/bin/flask", line 8, in <module>
sys.exit(main())
File "/usr/local/lib/python3.6/dist-packages/flask/cli.py", line 966, in main
cli.main(prog_name="python -m flask" if as_module else None)
File "/usr/local/lib/python3.6/dist-packages/flask/cli.py", line 586, in main
return super(FlaskGroup, self).main(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 782, in main
rv = self.invoke(ctx)
File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1259, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 1066, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/local/lib/python3.6/dist-packages/click/core.py", line 610, in invoke
return callback(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/click/decorators.py", line 21, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/flask/cli.py", line 425, in decorator
with __ctx.ensure_object(ScriptInfo).load_app().app_context():
File "/usr/local/lib/python3.6/dist-packages/flask/cli.py", line 388, in load_app
app = locate_app(self, import_name, name)
File "/usr/local/lib/python3.6/dist-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/arthur/Development/ISLWeb/islweb.py", line 1, in <module>
from app import app, db
File "/home/arthur/Development/ISLWeb/app/__init__.py", line 24, in <module>
from app import routes, models
File "/home/arthur/Development/ISLWeb/app/routes.py", line 11, in <module>
from app.forms import (LoginForm, RegistrationForm, CreateLaunchForm,
File "/home/arthur/Development/ISLWeb/app/forms.py", line 93, in <module>
class NewActionForm(FlaskForm):
File "/home/arthur/Development/ISLWeb/app/forms.py", line 95, in NewActionForm
users = User.query.all()
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/query.py", line 3186, in all
return list(self)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/query.py", line 3342, in __iter__
return self._execute_and_instances(context)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/orm/query.py", line 3367, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 988, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/sql/elements.py", line 287, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 1107, in _execute_clauseelement
distilled_params,
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 1253, in _execute_context
e, statement, parameters, cursor, context
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 1473, in _handle_dbapi_exception
util.raise_from_cause(sqlalchemy_exception, exc_info)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/util/compat.py", line 398, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/util/compat.py", line 152, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/base.py", line 1249, in _execute_context
cursor, statement, parameters, context
File "/usr/local/lib/python3.6/dist-packages/sqlalchemy/engine/default.py", line 580, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
[SQL: SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.password_hash AS user_password_hash, user.has_admin_rights AS user_has_admin_rights, user.is_customer AS user_is_customer
FROM user]
(Background on this error at: http://sqlalche.me/e/e3q8)
this is strange to me, because:
the table user is (and has been) there
I have been able to add columns to the User model before
I can still add columns to other models without errors
Now, I have seen this sqlite3.OperationalError coming up in many question here and elsewhere, but none of them seem to relate to the situation at hand here, as the codebase was working fine before, and so did migrations (they still do with all other models as well) Also, I have tried as a manner of test to delete the migrations folder as well as the database file, starting anew with:
flask db init
Which in my understanding starts a whole DB anew (including new migration script). Funnily enough this throws the exact same error as above. Which I also do not understand at all, as there is no db to read from.
My user model is as follows (with the column I'm trying to add commented):
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
actions = db.relationship('Action', backref='owner', lazy='dynamic')
events = db.relationship('WebLogEvent', backref='user', lazy='dynamic')
has_admin_rights = db.Column(db.Boolean)
is_customer = db.Column(db.Boolean)
#permissions = db.Column(db.String(256), index=True)
def __repr__(self):
return '<User {}>'.format(self.username)
#staticmethod
def table_header():
return ["ID", "Username", "e-mail", "Admin"]
def set_password(self, password):
self.password_hash = generate_password_hash(password)
def check_password(self, password):
return check_password_hash(self.password_hash, password)
def as_table_row(self):
return [self.id, self.username, self.email, self.has_admin_rights]
Any thoughts on what could be the matter here are most welcome.
You are issuing a query in the global scope of the application, in class NewActionForm. The query is User.query.all(), according to your stack trace.
The problem with this is that code that is in the global scope executes at import time. For many things this is okay, but database access is usually problematic, because you have to create and configure your Flask application instance before the database is accessible.
So the solution is to not query the database in the global scope. You can probably move that logic into the form's constructor method.

SQLAlchemy - Broken Pipe ERROR when trying to store large BLOB (i.e. machine learning model) to MySQL database

I would like to serialize many Gensim library Word2Vec models in MySQL database. For the purpose of my project I am utilizing: Python, Flask, SQLAlchemy, and MySQL+PyMySQL. Below you can see my Word2VecModel class which I am using in order to create word2vec table in MySQL DB:
from db.db import db
import db_models.influencer
class Word2VecModel(db.Model):
__tablename__ = "word2vec"
m_id = db.Column(db.Integer, primary_key=True)
m_username = db.Column(db.String(20), db.ForeignKey("influencer.m_username"))
m_binary = db.Column(db.LargeBinary)
influencer = db.relationship("InfluencerModel")
def __init__(self, m_binary, m_username):
self.m_binary = m_binary
self.m_username = m_username
def __repr__(self):
return '<Word2Vec {}>'.format(self.m_name)
#classmethod
def find_by_username(cls, m_username):
return cls.query.filter_by(name=m_username).first()
def save_to_db(self):
db.session.add(self)
db.session.commit()
def delete_from_db(self):
db.session.delete(self)
db.session.commit()
Whenever I try to execute:
word2vec_model = Word2VecModel(pickle.dumps(word2vec), username)
word2vec_model .save_to_db()
I get the following error message:
Traceback (most recent call last):
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 705, in _write_bytes
self._sock.sendall(data)
BrokenPipeError: [Errno 32] Broken pipe
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 773, in _execute_command
self.write_packet(sql[:packet_size])
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 634, in write_packet
self._write_bytes(data)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 710, in _write_bytes
"MySQL server has gone away (%r)" % (e,))
pymysql.err.OperationalError: (2006, "MySQL server has gone away (BrokenPipeError(32, 'Broken pipe'))")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "app.py", line 117, in <module>
word2vec.save_to_db()
File "/home/stefan/PycharmProjects/NLP - Influencer Text Analysis/src/flask_api/db_models/word2vec.py", line 25, in save_to_db
db.session.commit()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/scoping.py", line 163, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1042, in commit
self.transaction.commit()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 504, in commit
self._prepare_impl()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 483, in _prepare_impl
self.session.flush()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2523, in flush
self._flush(objects)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2664, in _flush
transaction.rollback(_capture_exception=True)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 69, in __exit__
exc_value, with_traceback=exc_tb,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2624, in _flush
flush_context.execute()
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 422, in execute
rec.execute(self)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/unitofwork.py", line 589, in execute
uow,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 245, in save_obj
insert,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/orm/persistence.py", line 1136, in _emit_insert_statements
statement, params
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1014, in execute
return meth(self, multiparams, params)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1133, in _execute_clauseelement
distilled_params,
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1318, in _execute_context
e, statement, parameters, cursor, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1512, in _handle_dbapi_exception
sqlalchemy_exception, with_traceback=exc_info[2], from_=e
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
raise exception
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1278, in _execute_context
cursor, statement, parameters, context
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 593, in do_execute
cursor.execute(statement, parameters)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
result = self._query(query)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/cursors.py", line 321, in _query
conn.query(q)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 504, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 773, in _execute_command
self.write_packet(sql[:packet_size])
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 634, in write_packet
self._write_bytes(data)
File "/home/stefan/.virtualenvs/dl4cv/lib/python3.6/site-packages/pymysql/connections.py", line 710, in _write_bytes
"MySQL server has gone away (%r)" % (e,))
Killed
I've read somewhere that increasing MySQL connection timeout might help, but not in my case. Here is my DB engine configuration:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:''#localhost/********'
app.config['PROPAGATE_EXCEPTIONS'] = True
app.config["SQLALCHEMY_ENGINE_OPTIONS"] = {"pool_recycle": 120}

Issue around connecting to a MySQL database table

I have some code which tries to connect to a mySQL database and retrieve a query.
The connection code is in a module called connection and looks like:
#!/usr/bin/env python3
import pandas as pd
import pymysql
from sqlalchemy import create_engine
COMPANY_SERVER = create_engine(
'mysql+pymysql://view_user:connectiondetails')
def read_sql(query):
return pd.read_sql_query(query, COMPANY_SERVER)
I have omitted the connection details for security. But this connection has worked in the past.
I am trying to connect and query a database table (which is pulled in with variable name table) and trying returning a DESCRIBE of the table in the following way:
#!/usr/bin/env python3
import datetime as dt
import numpy as np
import pandas as pd
from copy import copy
import pymysql
from sql.connection import read_sql
class DBSnapData(object):
def __init__(self,
start_dt,
end_dt,
start_point,
end_point,
variables,
table,
stock_id
):
self.start_dt = start_dt
self.end_dt = end_dt
self.start_point = start_point
self.end_point = end_point
self.variables = variables
self.table = table
self.stock_id = stock_id
self.schema = self.load_schema()
def load_schema(self):
return run_schema_sql_query(self.table)
def run_schema_sql_query(table):
return read_sql('DESCRIBE %s' % table)
I am getting the following error output:
2018-01-17 22:34:02,717 (pymysql.err.OperationalError) (1142, "SELECT command denied to user 'view_user'#'192.168.9.132' for table 'TBL_MKTDATA_SNAPS_AUS'") [SQL: 'DESCRIBE TBL_MKTDATA_SNAPS_AUS']
Traceback (most recent call last):
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/site-packages/pymysql/err.py", line 107, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.OperationalError: (1142, "SELECT command denied to user 'view_user'#'192.168.9.132' for table 'TBL_MKTDATA_SNAPS_AUS'")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/scoleman/bin/export_mkt_data", line 405, in main
extract_by_id(sc_id, bbg_ticker, bb_id, lot_size_changes)
File "/home/scoleman/bin/export_mkt_data", line 346, in extract_by_id
stock_id)
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 100, in __init__
self.schema = self.load_schema()
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 106, in load_schema
return run_schema_sql_query(self.table)
File "/home/scoleman/git/sql_data/sx_data_adhoc.py", line 19, in run_schema_sql_query
return read_sql('DESCRIBE %s' % table)
File "/home/scoleman/git/sql_data/sql/connection.py", line 17, in read_sql
return pd.read_sql_query(query, COMPANY_SERVER)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 331, in read_sql_query
parse_dates=parse_dates, chunksize=chunksize)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 1084, in read_query
result = self.execute(*args)
File "/usr/local/lib64/python3.5/site-packages/pandas/io/sql.py", line 975, in execute
return self.connectable.execute(*args, **kwargs)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 2064, in execute
return connection.execute(statement, *multiparams, **params)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 939, in execute
return self._execute_text(object, multiparams, params)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1097, in _execute_text
statement, parameters
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
exc_info
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
raise value.with_traceback(tb)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
context)
File "/usr/local/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 166, in execute
result = self._query(query)
File "/usr/local/lib/python3.5/site-packages/pymysql/cursors.py", line 322, in _query
conn.query(q)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 856, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1057, in _read_query_result
result.read()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1340, in read
first_packet = self.connection._read_packet()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 1014, in _read_packet
packet.check_error()
File "/usr/local/lib/python3.5/site-packages/pymysql/connections.py", line 393, in check_error
err.raise_mysql_exception(self._data)
File "/usr/local/lib/python3.5/sit
If anyone could give me a pointer as to a solution it would be much appreciated. I think the problem might be with pymysql in the connection but really am unsure.
Thanks
Your error says view_user doesn't have permission to query data on 192.168.9.132 this server.
You will have to do
GRANT SELECT ON *.* TO 'view_user'#'host_or_wildcard' IDENTIFIED BY 'password'; to allow your user to query on specified server.
Or
GRANT ALL PRIVILEGES ON *.* TO 'view_user'#'192.168.9.132'; to grant all priviliges.
P.S. To do above you will have to be root user. Use mysql -u root -p command to connect to mysql server.

Categories