sqlalchemy

sqlalchemy with postgres: insert into a table whose columns have parentheses

时光总嘲笑我的痴心妄想 提交于 2021-02-07 08:01:36
问题 Suppose you have a table "foo" in postgres with column name "col (parens) name" . The psql command INSERT INTO "foo" ("col (parens) name") VALUES ('bar'); works just fine. However, if I try to do the same using sqlalchemy (version 0.9.7), the resulting python code fails: conn = sqlalchemy.create_engine('postgresql://name:password@host:port/database') meta = sqlalchemy.schema.MetaData() meta.reflect(bind=conn) foo = meta.tables['foo'] vals = [{'col (parens) name': 'hi'}, {'col (parens) name':

How do I change column type on SQLAlchemy declarative model dynamically?

拜拜、爱过 提交于 2021-02-07 07:40:25
问题 I am running mysql in production but would like to run a simple tests in a sqlite in memory db. The legacy mysql db has tables with columns that are mysql specific types, Which are declared in declarative models (subclassing declarative_base). I would like to run some simple tests without going to mysql and so would need to swap out the columns of the model. How do I do this? I've tried writing a patcher/unpatcher to swap out table in my model, but when I run some tests, I get

How to explicitly load relationships on an existing object?

天涯浪子 提交于 2021-02-07 07:14:59
问题 I have a SQLAlchemy model Foo which contains a lazy-loaded relationship bar which points to another model that also has a lazy-loaded relationship foobar . When querying normally I would use this code to ensure that all objects are loaded with a single query: session.query(Foo).options(joinedload('bar').joinedload('foobar')) However, now I have a case where a base class already provides me a Foo instance that was retrieved using session.query(Foo).one() , so the relationships are lazy-loaded

How to explicitly load relationships on an existing object?

假装没事ソ 提交于 2021-02-07 07:11:17
问题 I have a SQLAlchemy model Foo which contains a lazy-loaded relationship bar which points to another model that also has a lazy-loaded relationship foobar . When querying normally I would use this code to ensure that all objects are loaded with a single query: session.query(Foo).options(joinedload('bar').joinedload('foobar')) However, now I have a case where a base class already provides me a Foo instance that was retrieved using session.query(Foo).one() , so the relationships are lazy-loaded

How to explicitly load relationships on an existing object?

假如想象 提交于 2021-02-07 07:07:21
问题 I have a SQLAlchemy model Foo which contains a lazy-loaded relationship bar which points to another model that also has a lazy-loaded relationship foobar . When querying normally I would use this code to ensure that all objects are loaded with a single query: session.query(Foo).options(joinedload('bar').joinedload('foobar')) However, now I have a case where a base class already provides me a Foo instance that was retrieved using session.query(Foo).one() , so the relationships are lazy-loaded

Sqlalchemy - rollback when exception

谁说我不能喝 提交于 2021-02-07 06:56:20
问题 I need to rollback a transaction in core.event 'handle_error' (catch 'KeyboardInterrupt') , but the parameter in this event is ExceptionContext , how do this? 回答1: I usually have this kind of pattern when working with sqlalchemy: session = get_the_session_one_way_or_another() try: # do something with the session except: # * see comment below session.rollback() raise else: session.commit() To make things easier to use, it is useful to have this as a context manager: @contextmanager def get

Generate Python Class and SQLAlchemy code from XSD to store XML on Postgres

*爱你&永不变心* 提交于 2021-02-07 04:24:13
问题 I have some very complex XSD schemas to work with. By complex I mean that each of these XSD would correspont to about 20 classes / tables in a database, with each table having approximately 40 fields. And I have 18 different XSD like that to program. What I'm trying to achieve is: Get a XML file defined by the XSD and save all the data in a PostgreSQL database using SQLAlchemy. Basically I need a CRUD application that will persist a XML file in the database following the model of the XSD

Generate Python Class and SQLAlchemy code from XSD to store XML on Postgres

北慕城南 提交于 2021-02-07 04:24:03
问题 I have some very complex XSD schemas to work with. By complex I mean that each of these XSD would correspont to about 20 classes / tables in a database, with each table having approximately 40 fields. And I have 18 different XSD like that to program. What I'm trying to achieve is: Get a XML file defined by the XSD and save all the data in a PostgreSQL database using SQLAlchemy. Basically I need a CRUD application that will persist a XML file in the database following the model of the XSD

SQLAlchemy declarative many-to-many self-join via Association object

我是研究僧i 提交于 2021-02-06 18:36:43
问题 I have a table Users and a table Friends which maps users to other users as each user can have many friends. This relation is obviously symmetric: if user A is a friend of user B then user B is also a friend of user A, I only store this relation once. The Friends table has additional fields besides the two User ID's so I have to use an association object. I am trying to define this relationship in declarative style in the Users class (which extends the declarative base), but I can't seem to

Rollback Many Transactions between tests in Flask

生来就可爱ヽ(ⅴ<●) 提交于 2021-02-06 15:52:53
问题 My tests take a long time to run and I am trying to rollback transactions between tests instead of dropping and creating the tables between tests. The issues is that in some tests I do multiple commits. EDIT: How do I rollback transactions between tests so that tests will run faster Here is the Base class used for testing. import unittest from app import create_app from app.core import db from test_client import TestClient, TestResponse class TestBase(unittest.TestCase): def setUp(self): self