On Mon, May 3, 2021, at 12:31 PM, Richard Damon wrote: > > > Ok, understand. Maybe my application is a bit unusual, but it seems I > will be doing this a LOT. >
that's fine, you'll make a function to do what you need and use it everywhere > > In my case nothing will REALLY be the base > class, but many of the sub-classes will have relations referencing the > id number of in the base class, and when importing a change set from an > external source, those id numbers might not match, so the base class > includes a UUID to match things up, and to avoid foreign key errors in > some of the derived objects, it seems I want to first make a pass to > create all the new nodes as just the base class so they don't create > broken relationships, and then upgrade them to their final type so I can > fill in the relationships. Since many of the relationships will by > cycles, I can't start at the base and build up. you could maybe use Core to create the rows for the new records without the relationships then use normal ORM techniques for the second pass. you can manufacture "persistent" objects that you didnt actually load from the database by building the Python object that looks like the database row(s), converting it to be "detached" using transient_to_detached (https://docs.sqlalchemy.org/en/14/orm/session_api.html?highlight=transient_to_detached#sqlalchemy.orm.make_transient_to_detached) , then you can session.add() that "detached" object into the Session and the Session will see it as an object that it loaded from the database. > > I presume that after changing the base record I should tell SQLAlchemy > to flush the old record out of its cache so it will re-read it with its > new identity. changing the class of a record doesn't change its "identity" from a PK perspective, but if you mean its pk + class, then yes. Or the above transient-to-detached step may save you some round trips. > > -- > Richard Damon > > -- > SQLAlchemy - > The Python SQL Toolkit and Object Relational Mapper > > http://www.sqlalchemy.org/ > > To post example code, please provide an MCVE: Minimal, Complete, and > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > description. > --- > You received this message because you are subscribed to the Google Groups > "sqlalchemy" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to sqlalchemy+unsubscr...@googlegroups.com > <mailto:sqlalchemy%2Bunsubscribe%40googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sqlalchemy/d7b66178-52bd-60cc-b8bd-655d46e535af%40Damon-Family.org. > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/2ebfc640-b8ed-4fd4-9dc8-428c36aac5ab%40www.fastmail.com.