by

Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Erol
    @FrEaKmAn
    I do not wish to create this a property as I would like to use the result in the query (for ordering)
    Federico Caselli
    @CaselIT
    I don't think it needs select
    From the docs, it seems to be working also with select. I'm not familiar with it, sorry
    Erol
    @FrEaKmAn
    without select I get Don't know how to convert the SQLAlchemy field get_product_price()
    Federico Caselli
    @CaselIT
    with func.get_product_price() does not work I guess?
    Erol
    @FrEaKmAn
    sorry, no.. maybe I need to cast it?
    this worked for me, but I'm not sure why I need to cast it
        base_price = column_property(
            cast(func.get_product_price(), Numeric(15, 5)).label('base_price')
        )
    Federico Caselli
    @CaselIT
    I've done a could of tests, and it seems to work with just func.get_product_price()
    Erol
    @FrEaKmAn
    can you please share your function?
    I'm using postgres
    Federico Caselli
    @CaselIT
    engine = sa.create_engine("sqlite://")
    engine.echo = True
    Session = sessionmaker(bind=engine)
    Base = declarative_base()
    class Foo(Base):
        __tablename__ = "foo"
        id = sa.Column(sa.Integer, primary_key=True)
        bar = sa.Column(sa.Text)
        c2 = orm.column_property(sa.func.date())
    Base.metadata.create_all(engine)
    s = Session()
    s.add(Foo(bar="123"))
    s.commit()
    r = s.query(Foo).all()
    print(vars(r[0]))
    this prints {'id': 1, 'bar': '123', 'c2': '2020-05-14'}
    get_product_price should be analogous to date in this example
    Erol
    @FrEaKmAn
    hm, ok...I'll check
    I have another quick question, could I somehow pass the table alias to function? for example
    Federico Caselli
    @CaselIT
    sqlite accepts date without parameters, so that should work
    Erol
    @FrEaKmAn
    class Product(db.Model):
        base_price = column_property(
            cast(func.get_product_price(Product), Numeric(15, 5)).label("base_price")
        )
    working SQL query is
    SELECT *, get_product_price(p) FROM products p;
    Federico Caselli
    @CaselIT
    not sure. passing 'products' does not what you want I guess
    Erol
    @FrEaKmAn
    nop :)
    I think it should be solved with alias/aliased.. I will try it tomorrow, @CaselIT thanks for help
    Federico Caselli
    @CaselIT
    np
    Structure and Interpretation of Computer Programs
    @metaperl
    Hello, what are the suggested ways to encrypt/hide the credentials that SQLAlchemy needs to access an Oracle database?
    Federico Caselli
    @CaselIT
    I think that an usual one is to use env variables
    Structure and Interpretation of Computer Programs
    @metaperl
    hmm, maybe.
    Federico Caselli
    @CaselIT
    Maybe you can also keys similar to ssh to connect. I know that that works on postgress
    Justvuur
    @Justvuur
    Hi All, I've been manually encrypting a field using an algorithm i created in python. A couple of months have passed and the requirements has changed quite a bit. I now need to encrypt several columns and I need to be able to perform queries/filtering on these columns. Is there a way to do such a thing? I tried creating a hybrid property that decrypts the field but that doesnt work in the queries. Is it possible? Any ideas? I'm using SQLAlchemy = 1.2
    PostgreSQL = 11.2.
    NomeChomsky
    @NomeChomsky
    I' confused
    when I create a new Signal() object, it needs to be linked to another object MarketInfo() - but this object may not yet exist. If it doesn't exist, I'd like to create it
    I wrote this into init:
        def __init__(self):
            market_info_obj = db.query(MarketInfo).filter_by(market=self.market).first()
            if not market_info_obj:
                market_info = bclient.get_symbol_info(self.market)
                new_market_info_obj = MarketInfo(market=self.market, info=market_info)
                db.add(new_market_info_obj)
                db.commit()
                new_market_info_obj.signals.append(self)
            else:
                market_info_obj.signals.append(self)
                db.commit()
    the issue is - this seems to over write the entire functionality of the Class, where as previously i could unpack a dictionary to create the object, I now cannot.
    NomeChomsky
    @NomeChomsky
    Got the answer - I need to call super().init() first so I don't overwrite the Base class init method
    Padam Sethia
    @PadamSethia

    Hi , I have a query on deleting data from Many to many relationships with cascade -> https://www.reddit.com/r/flask/comments/gntfd4/deleting_data_from_association_tables_when/

    Can anyone help ?

    Justvuur
    @Justvuur
    Hi All, so I made some progress... I managed to use a custom type to make use of my encryption. I have one last thing I'm stuck on... That's the decrypting if a field/column at query time. Can anyone help? So what I need is to be able to query the encrypted column... Person.query.filter(Person.name == 'Apples').first(); <--- That doesn't work because the field is still encrypted:-(
    Lungati's Projects
    @lungati
    session = self.db.get_session()
            pipeline = Pipeline(db=session)
            supp = pipeline.sup(data)
    ...
    class Pipeline(object):
        def __init__(self, db):
            self.session = db
       def cre(self, far, har_id=None):
            session = self.session
            session.begin(subtransactions=True)
            if not har_id:
                har = (
                    session.query(Hara).filter(
    ...
        def create_sup(self, data):
            session = self.session
            session.begin(subtransactions=True)
            schema = Sup()
            user_record = self.crer(data)
    I have the code above and I'm struggling to use nameko_sqlalchemy's sessionmaker object with subtransactions. No records being created in the database. Is there a way to check what's about to be flushed?
    carlobarbieri
    @carlobarbieri
    Hello all!
    is it possible to use in_ with a column from a subquery?
    what I'm thinking about is something along the lines of
    SELECT (SELECT col FROM table WHERE condition) IN (1, 2, 3) AS alias
    FROM anothertable
    Federico Caselli
    @CaselIT
    check as_scalar
    carlobarbieri
    @carlobarbieri
    grazie Federico, I'm gonna try that. However I suspect that won't work well if I have mure than one column and try using tuple_. Correct?
    carlobarbieri
    @carlobarbieri
    nevermind, it does work!
    carlobarbieri
    @carlobarbieri
    (at least in sqlite)
    Steven Garcia
    @sjosegarcia
    Hey everyone. So I am trying to figure out how to access the table object from a defined User model. It says I can just call table, but I do not see that function available.
    from typing import Any
    
    from sqlalchemy import Column, Integer, String, Boolean, Date, inspect
    
    from sqlalchemy.ext.declarative import declared_attr, as_declarative
    from sqlalchemy.ext.hybrid import hybrid_property
    
    
    @as_declarative()
    class Base:
        id: Any
        __name__: str
    
        @declared_attr
        def __tablename__(self) -> str:
            return self.__name__.lower()
    
    
    class User(Base):
    
        id = Column(Integer, primary_key=True, index=True)
        email = Column(String, unique=True, index=True)
        username = Column(String, nullable=True)
        password = Column(String, nullable=True)
        first_name = Column(String, nullable=True)
        last_name = Column(String, nullable=True)
        birth_date = Column(Date, nullable=True)
        is_active = Column(Boolean, default=True)
    
        @hybrid_property
        def full_name(self) -> str:
            return f"{self.first_name} {self.last_name}"
    The query I want to run is
    select(User).where(User.username == "test_user")
    Federico Caselli
    @CaselIT
    The table is available as User.__table__. Note that the select will return tuples, not User objects. If you want User object you need to use a session
    Francisco Palomares
    @FranciscoPalomares
    Hi, I have this problem with my sql: 2013, 'Lost connection to MySQL server during query'
    Engine is:
     engine = sqlalchemy.create_engine(DATABASE_URI, echo=False, pool_recycle=1,pool_pre_ping=True)
    Jonathan Vanasco
    @jvanasco
    There is a section in the docs that deals with various approaches you can take for disconnects - https://docs.sqlalchemy.org/en/13/core/pooling.html#dealing-with-disconnects
    you're using the pool_pre_ping which is great for checking out connections, but you'll have to handle the recovery strategy for what happens when the server goes down mid-query/transaction.
    edwardtilley
    @edwardtilley
    I have a config question that might be a simple fix but I can't figure it out as a new user. I used the native SQLAlchemy python setup in my models.py to create and maintain a new User table, but I want my Flask app to work with an existing database for all other tables via an automap. My stackflow explanation has all of the configs I think ... any ideas?