Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Alex Bender
    @alex-bender
    is there a way to set this cascade behaviour if Sequence is being used outside of relation?
    ticket_number = db.Column( db.Integer, sequence_ticket_number, server_default=sequence_ticket_number.next_value(), autoincrement=True, nullable=False)
    Alex Grönholm
    @agronholm
    can you find such a thing in the documentation? https://www.postgresql.org/docs/current/sql-createsequence.html
    Alex Bender
    @alex-bender
    this is not exactly about setting that in SQL, this is about generating sql statement for deletion
    so as far as I can see it's python objects being compiled into sql
    Alex Grönholm
    @agronholm
    that declaration seems a bit odd – isn't it enough to specify the column type and sequence to be used? What are the autoincrement and server_default for?
    Alex Bender
    @alex-bender
    let me check it quickly
    Alex Grönholm
    @agronholm
    why are you pasting these links?
    Alex Bender
    @alex-bender
    you've asked me what is server_default for
    first link answers that
    Alex Grönholm
    @agronholm
    no, I asked why you had that in the column definition
    I know very well what these parameters do
    Alex Bender
    @alex-bender
    sorry, misunderstood your question
    Alex Grönholm
    @agronholm
    https://docs.sqlalchemy.org/en/14/core/defaults.html#sqlalchemy.schema.Sequence <- as you can see from the example, just having the sequence there is enough
    Alex Bender
    @alex-bender
    i see
    the code is legacy, trying to launch tests and droppping whole db is blocked because of problems during sequence deletion
    thanks, gonna try to remove
    Alex Bender
    @alex-bender
    that helped, thanks
    so problem was not at sql level, right?
    if removing this sequence_ticket_number.next_value() part resolves
    Alex Grönholm
    @agronholm
    it was on the table definition level methinks
    Alex Bender
    @alex-bender
    anyways that helped. So even wihout this server_default Sequence will be incrementing, right?
    Alex Grönholm
    @agronholm
    yup
    Alex Bender
    @alex-bender
    nice
    Safa Alfulaij
    @SafaAlfulaij

    Hi all.

    Using ORM.
    I have a O2M relationship that I set to lazy="raise" (addresses in User table).
    Now I want to use the relationship User.addresses in a query:
    result = await async_session.execute(User.addresses.statement)
    But it complains saying AttributeError: 'InstrumentedList' object has no attribute 'statement'

    I don't want to change the lazy so that I don't accidentally call it.

    Federico Caselli
    @CaselIT
    Don't know that that api is. Please see this for details on relationship loading https://docs.sqlalchemy.org/en/14/orm/loading_relationships.html?highlight=relationship+loading
    Safa Alfulaij
    @SafaAlfulaij

    Another question: How to joinedload and selectinload on the same table?
    Example:
    User - O2M - Addresses
    User - M2O (FK) - Group

    This doesn't work:
    select(User).options(joinedload(User.group).selectinload(User.addresses))

    Alex Grönholm
    @agronholm
    @SafaAlfulaij have you tried giving them as separate arguments to options()?
    chaining them indicates that you somehow want to load the "addresses" relationship in the related Group
    Federico Caselli
    @CaselIT

    select(User).options(joinedload(User.group).selectinload(User.addresses))

    you should not chain them since aggresses is not part of Group. they should be separated by comma, since are both "top level" (of user) select(User).options(joinedload(User.group), selectinload(User.addresses))

    Safa Alfulaij
    @SafaAlfulaij
    Thanks both, figured that after a while.
    I'm coming from Django ORM background, so please be patient with me :)
    Federico Caselli
    @CaselIT
    No problem!
    Will Shamblin
    @wcshamblin
    Hi guys, I have a problem that I'm not sure how to approach.
    I have 3 classes that all share some attributes - (id, entry time, owner, category) and I would like to be able to query them all at once.
    So say I need to search for class where entry time < datetime I could easily retrieve all classes where that would be the case. Not knowing much about sqlalchemy I figured the easiest solution would be an abstract class that would then be used for querying but it seems that is not supported?
    Right now I'm stuck because the only querying that seems to be supported is through one class at a time - session.query(class).filter() but I need to be able to query through multiple classes by their shared attributes.
    Federico Caselli
    @CaselIT
    usually that classes are in defined in an hierarchy of some kind, otherwise the orm would not know how what class to return for each row.
    Pascal Brückner
    @sylencecc
    Hello! Using async SQLAlchemy, let's assume I have all my relationships defined with lazy="raise", which would raise an exception each time I try to access the respective attribute. Is there a way to load such an "unloaded" relationship after I have already obtained the object from the ORM? According to the documentation, Session.refresh()doesn't automatically refresh unloaded relationships. How do I load the relationship, then? I'm aware of joinedload() & friends, but in case of a large object graph I would like to avoid eagerly loading the whole database with every single query.
    Federico Caselli
    @CaselIT
    there may be an api missing from refresh. In the meantime I believe you can use session.get with populate_existing=True since that takes a list of options where you can specify loader
    it's also in the "tip" box in the refresh documenation
    Pascal Brückner
    @sylencecc
    Thanks, I'll give that a try!
    wnesbv
    @wnesbv
    Good afternoon. Share your experience with those who are familiar with the situation: only the first line from the csv file is added..
            async with aiofiles.open(
                directory, mode="r", encoding="utf-8", newline=""
            ) as afp:
                async for new in AsyncDictReader(afp):
                    query = ScheduleService(
                        **{
                            "id": new["id"],
                            "name": new["name"],
                            "type": new["type"],
                            "title": new["title"],
                            "description": new["description"],
                            "date": datetime.strptime(
                                new["date"], settings.DATE
                            ).date(),
                            "there_is": datetime.strptime(
                                new["there_is"], settings.DATETIME_FORMAT
                            ),
                            "created_at": datetime.now(),
                            "sch_s_owner": new["sch_s_owner"],
                            "sch_s_service_id": new["sch_s_service_id"],
                        }
                    )
                    # ..
                    session.add_all(query)
                    session.refresh([query])
                    await session.commit()
    Anler
    @anler
    Hi all, I'm trying to follow the tutorial on caching (sqlalchemy version 1.3): https://docs.sqlalchemy.org/en/13/_modules/examples/dogpile_caching/caching_query.html but whenever I execute the code line: self.mergte_result(..., load=False) I get: AttributeError: '<MyModel>' object has no attribute '_sa_instance_state' does anyone know if this is me making a mistake/wrong-assumption or is the tutorial that is missing something?
    Jonathan Vanasco
    @jvanasco
    The example code changed heavily with the 1.4 release. I'm not sure if the 1.3 docs worked on the 1.3 version, or if they were from an earlier version. In any case, you should be using 1.4 as 1.3 is approaching End of Life
    jstevans
    @jstevans:matrix.org
    [m]
    Is there a way to perform the value transformations on my ORM object that happen when calling db.session.commit(), without needing an active DB connection?
    In my case, I have a class MyModel(sqlalchemy.Model) with type = db.Column(db.Text()) and I assign myModelObj.type = MyTypeEnum.MY_TYPE1. Before I do db.session.commit(), I see myModelObj.type == MyTypeEnum.MY_TYPE1 -- but after I do db.session.commit(), a transformation has happened and myModelObj.type == "MY_TYPE1"
    jstevans
    @jstevans:matrix.org
    [m]
    I believe what's happening is that SQLAlchemy triggers a read from the DB, by deleting the underlying state from my ORM object, which triggers a reload on the next state access, which seems to hit the DB
    Federico Caselli
    @CaselIT
    It seem to work as expected. If you want the enum in the column you should use the enum column