sqlalchemy

SQLAlchemy, polymorphic inheritance with classical mapping [duplicate]

微笑、不失礼 提交于 2021-01-28 07:00:52
问题 This question already has an answer here : Need classic mapper example for SqlAlchemy single table inheritance (1 answer) Closed 3 years ago . I need to use the classical mapping instead of the declarative, for the last two days I am trying to make inheritance work, I tried with the declarative style and it worked but whatever I tried I cant get it to work when using the old mapping style. class Item(object): def specialised_method(self): return "I am not special" class SpecialisedItem(Item):

Improve pandas' to_sql() performance with SQL Server

拈花ヽ惹草 提交于 2021-01-28 06:31:22
问题 I come to you because i cannot fix an issues with pandas.DataFrame.to_sql() method. I've made the connection between my script and my database, i can send queries, but actually it's too slow for me. I would like to find a way to improve the performance of my script on this. Maybe someone will find a solution? Here is my code : engine = sqlalchemy.create_engine(con['sql']['connexion_string']) conn = engine.connect() metadata = sqlalchemy.Metadata() try : if(con['sql']['strategy'] == 'NEW'):

SQLAlchemy: How to represent data in Python differently than in the database

让人想犯罪 __ 提交于 2021-01-28 06:02:22
问题 In a (postgres) SQLAlchemy model/class, I have several columns that are 'price' columns. I have read that using numeric/money/float types for this sort of data is not a good idea, so I'm storing as INTs (pennies). I have created validators for these columns that will multiply the input value by 100 and cast to an INT upon insert and update, so that is taken care of. How do I do the reverse? When I select this data, I want to cast these values to a float and then divide by 100. I cannot seem

SQLAlchemy: How to represent data in Python differently than in the database

假装没事ソ 提交于 2021-01-28 05:53:16
问题 In a (postgres) SQLAlchemy model/class, I have several columns that are 'price' columns. I have read that using numeric/money/float types for this sort of data is not a good idea, so I'm storing as INTs (pennies). I have created validators for these columns that will multiply the input value by 100 and cast to an INT upon insert and update, so that is taken care of. How do I do the reverse? When I select this data, I want to cast these values to a float and then divide by 100. I cannot seem

Python - How to connect SQLAlchemy to existing database in memory

徘徊边缘 提交于 2021-01-28 04:40:19
问题 I'm creating my DB from an existing shema and it's stored in :memory: . db = Database(filename=':memory:', schema='schema.sql') db.recreate() I now want to "link" this to SQL Alchemy. Followed different methods but could not get it right. My current attempt stands as follow: engine = create_engine('sqlite:///:memory:') Base = automap_base() Base.prepare(engine, reflect=True) User = Base.classes.user session = Session(engine) Much like the other stuff I tried this will throw AttributeError:

Python - How to connect SQLAlchemy to existing database in memory

浪子不回头ぞ 提交于 2021-01-28 04:31:00
问题 I'm creating my DB from an existing shema and it's stored in :memory: . db = Database(filename=':memory:', schema='schema.sql') db.recreate() I now want to "link" this to SQL Alchemy. Followed different methods but could not get it right. My current attempt stands as follow: engine = create_engine('sqlite:///:memory:') Base = automap_base() Base.prepare(engine, reflect=True) User = Base.classes.user session = Session(engine) Much like the other stuff I tried this will throw AttributeError:

contains_eager and limits in SQLAlchemy

拈花ヽ惹草 提交于 2021-01-28 04:06:07
问题 I have 2 classes: class A(Base): id = Column(Integer, primary_key=True) name = Column(String) children = relationship('B') class B(Base): id = Column(Integer, primary_key=True) id_a = Column(Integer, ForeignKey('a.id')) name = Column(String) Now I need all object A which contains B with some name and A object will contain all B objects filtered. To achieve it I build query. query = db.session.query(A).join(B).options(db.contains_eager(A.children)).filter(B.name=='SOME_TEXT') Now I need only

Perform alembic upgrade in multiple schemas

隐身守侯 提交于 2021-01-28 02:51:48
问题 I am using flask + sqlalchemy + alembic + postgresql, and am trying to find a simple architecture to solve the following problem. I have a simple database structure, lets say two tables : -- Users -- UserItems My users are in several different domains. I would like to have several of this database structure. For that I have database schemas in SQL. I created the matching class structure using sqlalchemy decalrative_base, and end up with a MetaData object that isn't tied to a specific schema.