sqlalchemy

How should I unit test MySQL queries?

我的未来我决定 提交于 2021-02-10 05:12:49
问题 I'm building some unit tests for my Python module which interfaces with a MySQL database via SQLAlchemy. From reading around I gather the best way to do this is to create a test database that I can query as if it was the real thing. I've done this however how should I test the existing queries in the module as they currently all point at the live database? The only idea I'd come up with was to do something like the following: def run_query(engine, db_name='live_db') engine.execute(f'SELECT *

Can I execute query via sqlalchemy without transaction

非 Y 不嫁゛ 提交于 2021-02-09 18:02:49
问题 I am trying to execute a stored procedure on Mysql database with sqlalchemy. It runs fine from the shell but throws this error: OperationalError: (MySQLdb._exceptions.OperationalError) (1568, "Transaction characteristics can't be changed while a transaction is in progress") The reason as it seems is that SQLAlchemy runs query within a transaction. And the transaction within the stored procedure is conflicting with it. Below is sqlalchemy log: 2019-07-24 15:20:28,888 INFO sqlalchemy.engine

Can I execute query via sqlalchemy without transaction

别等时光非礼了梦想. 提交于 2021-02-09 18:02:25
问题 I am trying to execute a stored procedure on Mysql database with sqlalchemy. It runs fine from the shell but throws this error: OperationalError: (MySQLdb._exceptions.OperationalError) (1568, "Transaction characteristics can't be changed while a transaction is in progress") The reason as it seems is that SQLAlchemy runs query within a transaction. And the transaction within the stored procedure is conflicting with it. Below is sqlalchemy log: 2019-07-24 15:20:28,888 INFO sqlalchemy.engine

Can I execute query via sqlalchemy without transaction

和自甴很熟 提交于 2021-02-09 18:01:22
问题 I am trying to execute a stored procedure on Mysql database with sqlalchemy. It runs fine from the shell but throws this error: OperationalError: (MySQLdb._exceptions.OperationalError) (1568, "Transaction characteristics can't be changed while a transaction is in progress") The reason as it seems is that SQLAlchemy runs query within a transaction. And the transaction within the stored procedure is conflicting with it. Below is sqlalchemy log: 2019-07-24 15:20:28,888 INFO sqlalchemy.engine

How to instantiate a table object to bulk_insert rows using alembic / SQLAlchemy

五迷三道 提交于 2021-02-08 14:03:52
问题 I am trying to use bulk_insert to insert data into an existing table ( services ) in my Postgres database. How do I instantiate this table object so I can do a bulk_insert with it? I saw answers like this: Alembic bulk_insert to table with schema but I want to avoid redefining the schema again in the migration. from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql def upgrade(): """Up migration.""" services = sa.MetaData().Services() op.bulk_insert(services

How to find the offending attribute with a sqlalchemy IntegrityError

天大地大妈咪最大 提交于 2021-02-08 12:40:07
问题 I have a very simple SqlAlchemy model class User(Base): """ The SQLAlchemy declarative model class for a User object. """ __tablename__ = 'users' id = Column(Integer, primary_key=True) phone = Column(String, unique=True) email = Column(String, unique=True) When inserting a new User, an IntegrityError could occur if the email or phone is a duplicate. Is there any way to detect which of the columns was violating the integrity error? Or is the only way to do a separate query to see or a value is

What is correct way to use Flask-SQLAlchemy in Flask server?

回眸只為那壹抹淺笑 提交于 2021-02-08 11:39:40
问题 This is model code, I have tested this code, it is no error and it can create tables, records in DB createdb.py app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@localhost:3306/ai' db = SQLAlchemy(app) if __name__ == 'createdb': db.reflect() db.drop_all() db = SQLAlchemy(app) class Class(db.Model): id = db.Column(db.Integer, primary_key=True, unique=True, autoincrement=True) label = db.Column(db.String(255), unique=True, nullable=False) def __init__(self,

Is it possible to use Azure AD on Ubuntu for connecting to Azure SQL using sqlalchemy and Python?

我只是一个虾纸丫 提交于 2021-02-08 11:33:55
问题 Is it possible to use Azure AD on Ubuntu for connecting to Azure SQL? That is, it is possible to use trusted_connection=True in sqlalchemy in Python? # Creating engine engine = sqlalchemy.create_engine('mssql://*server_name*/*database_name*?trusted_connection=yes') On Azure you can create a linux VM with a managed identity which allows you to connect to Azure services using Azure AD. In their documentation I can find examples of how to connect to various Azure services using this, however, I

DatabaseError : “not all arguments converted during string formatting” when I use pandas.io.sql.to_sql()

时光总嘲笑我的痴心妄想 提交于 2021-02-08 11:26:59
问题 I have a table: And I try to use this import this table by sqlalchemy , the code is: import sqlalchemy as db import pandas.io.sql as sql username = 'root' password = 'root' host = 'localhost' port = '3306' database = 'classicmodels' engine = db.create_engine(f'mysql+pymysql://{username}:{password}@{host}:{port}/{database}') con = engine.raw_connection() #readinto dataframe df = pd.read_sql(f'SELECT * FROM `{database}`.`offices`;', con) print(df[:2]) df_append = pd.DataFrame([{'officeCode': 8,

How can I get information from 2 separate tables with a single request in Flask?

折月煮酒 提交于 2021-02-08 11:26:30
问题 class posts(db.Model): __tablename__ = "posts" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) postName = db.Column(db.String(100)) postDescription = db.Column(db.String(500)) postLike = db.Column(db.Integer) class postComment(db.Model): __tablename__ = "postComment" id = db.Column(db.Integer, primary_key=True) postID = db.Column(db.Integer) senderName = db.Column(db.String(20)) commentPost = db.Column(db.String(300)) class PostsSchema(ma.Schema): class Meta: