sqlalchemy

SQL Query that relies on COUNT from another table? - SQLAlchemy

痞子三分冷 提交于 2021-01-28 22:23:30
问题 I need a query that can return the records from table A that have greater than COUNT records in table B. The query needs to be able to go in line with other filters that might be applied on table A. Example case study: I have a person and appointments table. I am looking for all people who have been in for 5 or more appointments. It must also support extra filter statements on the person table, such as age > 18. EDIT -- SOLUTION subquery = db.session.query(Appointment.id_person, func.count('*

Python MySQL connector returns bytearray instead of regular string value

三世轮回 提交于 2021-01-28 22:08:06
问题 I am loading data from one table into pandas and then inserting that data into new table. However, instead of normal string value I am seeing bytearray. bytearray(b'TM16B0I8') it should be TM16B0I8 What am I doing wrong here? My code: engine_str = 'mysql+mysqlconnector://user:pass@localhost/db' engine = sqlalchemy.create_engine(engine_str, echo=False, encoding='utf-8') connection = engine.connect() th_df = pd.read_sql('select ticket_id, history_date', con=connection) for row in th_df.to_dict

Why doesn't schema_translate_map change schema?

帅比萌擦擦* 提交于 2021-01-28 21:55:31
问题 I'm trying to use schema_translate_map to change a schema: Base = declarative_base() class DataAccessLayer(): def __init__(self): conn_string = "mysql+mysqlconnector://root:root@localhost/" self.engine = create_engine(conn_string) Session = sessionmaker() Session.configure(bind=self.engine) self.session = Session() def change_schema(self): self.session.connection(execution_options={"schema_translate_map": {"belgarath": "belgarath_test"}}) class Player(Base): __tablename__ = "player" __table

Why doesn't schema_translate_map change schema?

拜拜、爱过 提交于 2021-01-28 21:39:52
问题 I'm trying to use schema_translate_map to change a schema: Base = declarative_base() class DataAccessLayer(): def __init__(self): conn_string = "mysql+mysqlconnector://root:root@localhost/" self.engine = create_engine(conn_string) Session = sessionmaker() Session.configure(bind=self.engine) self.session = Session() def change_schema(self): self.session.connection(execution_options={"schema_translate_map": {"belgarath": "belgarath_test"}}) class Player(Base): __tablename__ = "player" __table

Stored procedure with Alembic: MySQL Syntax Error

旧城冷巷雨未停 提交于 2021-01-28 18:38:05
问题 I am using Alembic to load a stored procedure into the MySQL database. Following the cookbook, I was able to create the required objects for creating and dropping the procedure. Now that I want to upgrade the version to actually load the procedure, I am getting SQL syntax near the DELIMITER $$ which I need for procedure definition. I have also tried to remove the DELIMITER and replace AS for starting the procedure, but that didn't work either. I even tried the simple example function in the

Flask-SQLAlchemy & Google App Engine - no permission to write

北城余情 提交于 2021-01-28 13:50:13
问题 Main.py from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SECRET_KEY'] = '123' app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db = SQLAlchemy(app) class User(db.Model): __table_name__ = 'user' id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(80), unique=True) password = db.Column(db.String(80)) def __init__(self, username, password): self.username = username self.password =

SQLAlchemy Expression Language - INSERT SELECT with literal values

佐手、 提交于 2021-01-28 12:01:13
问题 I've just started using the SQLAlchemy SQL Expression Language, and I'm trying to insert into a table values from an existing tables and literal values. I'm trying to emulate this SQL statement in the SQL Expression language: INSERT INTO b_Customers (CustomerID, CustomerName, CustomerType, IsCurrent, FirstObserved, LastObserved) SELECT CustomerID, CustomerName, CustomerType, 1, '2018-01-23', '2018-01-23' FROM s_Customers Here's some setup Python code I'm running: from datetime import datetime

What happens if I specify OneToMany relationship only from one side in flask-sqlalchemy?

社会主义新天地 提交于 2021-01-28 11:41:43
问题 I have a OneToMany relationship between 2 entities in flask. I also specified the relationship only on one side. I am unsure what the difference is between the following: class CustomJob(db.Model): __tablename__ = "custom_job" id = db.Column(db.Integer, primary_key=True, autoincrement=True) country_from = db.Column(db.Integer, db.ForeignKey('country.id')) class Country(db.Model): __tablename__ = "country" id = db.Column(db.Integer, primary_key=True, autoincrement=True) custom_jobs = db

SQLAlchemy only func.rank() rows in table where value = x

时光毁灭记忆、已成空白 提交于 2021-01-28 11:33:21
问题 In my previous question, I managed to get the rank of a table based on its value in that table. I can now rank movies based on a given score (using the following table and method) : class MoviePersonScores(db.Model): movie_id = db.Column(db.Integer, db.ForeignKey('movie.id'), primary_key=True) person_id = db.Column(db.Integer, db.ForeignKey('person.id'), primary_key=True) score = db.Column(db.Integer) person = relationship("Person", back_populates="movies") movie = relationship("Movie", back

insert using pandas to_sql() missing data into clickhouse db

浪尽此生 提交于 2021-01-28 09:00:50
问题 It's my first time using sqlalchemy and pandas to insert some data into a clickhouse db. When I try to insert some data using clickhouse cli it works fine, but when I tried to do the same thing using sqlalchemy I don't know why one row is missing. Have I done something wrong? import pandas as pd # created the dataframe engine = create_engine(uri) session = make_session(engine) metadata = MetaData(bind=engine) metadata.reflect(bind = engine) conn = engine.connect() df.to_sql('test', conn, if