sqlalchemy

Passing parameters not being recognized and throws SQL error when executing raw query (on SQL-Server database and Pymssql) with SqlAlchemy

泪湿孤枕 提交于 2021-02-05 11:22:09
问题 I'm trying to execute simple raw SQL query, on SQL-Server db with SqlAlchemy (and Pymssql as provider). Here is my first try (using execute method of connection and passing parameters in **kwargs way): provider = DataProvider().engine q = "select url from Crawler.CrawlSource where source=@source" query = text(q) result = provider.connect().execute(query, source ='mysource') I passed the parameters in any way shown in tutorials (passing as kwargs and passing as dict) but neither of them worked

SQLAlchemy - Getting a list of tables

家住魔仙堡 提交于 2021-02-05 09:43:45
问题 I couldn't find any information about this in the documentation, but how can I get a list of tables created in SQLAlchemy? I used the class method to create the tables. 回答1: All of the tables are collected in the tables attribute of the SQLAlchemy MetaData object. To get a list of the names of those tables: >>> metadata.tables.keys() ['posts', 'comments', 'users'] If you're using the declarative extension, then you probably aren't managing the metadata yourself. Fortunately, the metadata is

how left outer join in sqlalchemy?

为君一笑 提交于 2021-02-05 09:21:58
问题 I want implement left outer join in sqlalchemy. the sql query is like this: select * from skills left join user__skill on user__skill.skill_id=skills.id and user__skill.user_id=4 and what i wrote in sqlalchemy is : skills = db.session.query(Skill, User_Skill.skill_id).\ outerjoin(User_Skill, User_Skill.skill_id==Skill.id and User_Skill.user_id==4).\ order_by(Skill.name).all() but it doesn't filter for a user and show all users skills. how can i write this code? 回答1: EDIT: Use and_ from

What's the difference between an association table and a regular table?

浪子不回头ぞ 提交于 2021-02-05 09:17:27
问题 I don't think I fully understand association tables. I know how to work with normal tables i.e add rows and what not but I don't understand how to work with an association table. why would I use the below student_identifier = db.Table('student_identifier', db.Column('class_id', db.Integer, db.ForeignKey('classes.class_id')), db.Column('user_id', db.Integer, db.ForeignKey('students.user_id')) ) Vs class studentIdent(db.model): db.Column(db.Integer, db.ForeignKey('classes.class_id')), db.Column

What's the difference between an association table and a regular table?

好久不见. 提交于 2021-02-05 09:15:10
问题 I don't think I fully understand association tables. I know how to work with normal tables i.e add rows and what not but I don't understand how to work with an association table. why would I use the below student_identifier = db.Table('student_identifier', db.Column('class_id', db.Integer, db.ForeignKey('classes.class_id')), db.Column('user_id', db.Integer, db.ForeignKey('students.user_id')) ) Vs class studentIdent(db.model): db.Column(db.Integer, db.ForeignKey('classes.class_id')), db.Column

Using function output in SQLAlchemy join clause

[亡魂溺海] 提交于 2021-02-05 08:25:15
问题 I am trying to translate a fairly short bit of SQL into an sqlAlchemy ORM query. The SQL uses Postgres's generate_series to make a set of dates and my goal is to make a set of time series arrays categorized by one of the columns. The tables (simplified) are very simple: counts: ----------------- count (Integer) day (Date) placeID (foreign key related to places) "counts_pkey" PRIMARY KEY (day, placeID) places: ----------------- id name (varchar) The output I'm after is a time series of counts

Why am I getting AmbiguousForeignKeysError?

喜你入骨 提交于 2021-02-05 07:56:06
问题 I've run into an issue after following the SqlAlchemy guide here. Given the following simplified module: class _Base(): id_ = Column(Integer, primary_key=True, autoincrement=True) Base = declarative_base(cls=_Base) class BlgMixin(): @declared_attr def __table_args__(cls): return {'schema': "belgarath_backup", "extend_existing": True} class DataAccessLayer(): def __init__(self): conn_string = "mysql+mysqlconnector://root:root@localhost/" self.engine = create_engine(conn_string) def create

How to prevent pandas dataframe from adding double quotes around #tmp when using sqlalchemy and sybase?

天大地大妈咪最大 提交于 2021-02-05 07:43:49
问题 I have reduced the issue to pandas to_sql adding double quotes around #tmp when dealing with sybase using sqlalchemy as the pooling framework. Code : def get_data_with_tmp(): engine = get_connection("sybase") with engine.connect() as conn: df = pd.DataFrame({'alias_id': ['345402KP5', '3454014R1']}) df.to_sql(name='#tmp', con=conn, schema=None, if_exists='append', index=False) df = pd.read_sql_query("SELECT alias_id from #tmp", con=conn) Error: statement = '\nCREATE TABLE "#tmp" (\n\talias_id

how to using dateadd in sqlalchemy with filter?

主宰稳场 提交于 2021-02-05 06:31:28
问题 The sql expression : select * from order where status=0 and adddate(created_time, interval 1 day)>now(); python code: from sqlalchemy.sql.expression import func, text from datetime import datetime closed_orders = DBSession.query(Order).filter(func.dateadd(Order.create_time, text('interval 1 day'))>datetime.now()).all() but it's got wrong. how to do it correctly? thanks REF :Using DATEADD in sqlalchemy 回答1: Try this: from sqlalchemy import func import datetime DBSession.query(Order)\ .filter

Can I append twice the same object to an InstrumentedList in SQLAlchemy?

廉价感情. 提交于 2021-02-05 06:22:05
问题 I have a pretty simple N:M relationship in SqlAlchemy 0.6.6. I have a class "attractLoop" that can contain a bunch of Media (Images or Videos). I need to have a list in which the same Media (let's say image) can be appended twice. The relationship is as follows: The media is a base class with most of the attributes Images and Videos will share. class BaseMedia(BaseClass.BaseClass, declarativeBase): __tablename__ = "base_media" _polymorphicIdentity = Column("polymorphic_identity", String(20),