[sqlalchemy] What is the standard/best way to merge an object graph into the session?

2019-04-24 Thread James Fennell
I have a parent child relationship which I construct from a data feed. At the time of constructing the object graph I don't have access to the primary keys of the entities, so I build up the object graph by using the relationship attributes. My understanding was that I could perform a session.m

[sqlalchemy] Re: What is the standard/best way to merge an object graph into the session?

2019-04-24 Thread James Fennell
Oooo the problem is not what I thought. The problem is that in my 'new data' there is no new_child_2. This is an expected case, as sometimes children disappear, so will update the post. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post

[sqlalchemy] Re: What is the standard/best way to merge an object graph into the session?

2019-04-24 Thread James Fennell
Okay let me answer my own question. The problem is that my parent-child relationship does not have the delete-orphan cascade. So when I set the new children, the old child_2 loses its parent (as is expected, because it's no longer a child) and then there's an error because the DB has a not null

[sqlalchemy] Re: Possible regression?

2019-04-28 Thread James Fennell
It seems to be related to the cascades happening recursively. The merge cascade goes from the tassel thread to the head, and then again down from the head to the tassel thread - which is kind of strange, I would expect the cascades to only visit each node in the object graph at most once. The s

Re: [sqlalchemy] Possible regression?

2019-04-28 Thread James Fennell
Thanks for the explanation Mike! Seeing it now, I actually think there’s a decent reason to want the current backerefs: My understanding is that with session.merge in SQL Alchemy it’s possible to draw a very clean line between entities that are persisted (or about to be persisted on the next fl

Re: [sqlalchemy] Re: Query last relation created and attributes

2019-05-13 Thread James Fennell
I think Mike's suggestion was to construct the raw SQL string you want, then reverse engineer to get the correct SQL Alchemy code, which you can then use with your different models. For complicated SQL logic I think this is a good practice in general. You current question seems like a general SQL

[sqlalchemy] Large RAM usage in bulk_insert_mappings

2019-11-14 Thread James Fennell
Hi all, Just sharing some perf insights into the bulk operation function bulk_insert_mappings. I was recently debugging a SQL Alchemy powered web app that was crashing due to out of memory issues on a small Kubernetes node. It turned out to be "caused" by an over optimistic invocation of bulk_ins

Re: [sqlalchemy] Large RAM usage in bulk_insert_mappings

2019-11-14 Thread James Fennell
been investigating > memory usage when loading data using memory_profiler and would be > interested to find out about the best approach > > On Thu, 14 Nov 2019, 17:16 James Fennell, wrote: > >> Hi all, >> >> Just sharing some perf insights into the bulk operation fu

Re: [sqlalchemy] Re: How to check for table's existance without a separate query

2019-12-04 Thread James Fennell
> However I cannot catch for this error, I can only catch for "sqlalchemy.exc.ProgrammingError". Why is that? James On Wed, Dec 4, 2019 at 6:03 PM Jonathan Vanasco wrote: > > Personally, I would handle the check like this: > > ERRORS_UNDEFINED_TABLE = (psycopg2.errors.UndefinedTable, ) > > t

[sqlalchemy] How can I use bulk_save_objects when I know the primary key?

2020-03-12 Thread James Fennell
Hey all! I'm having a problem using the bulk_save_objects function. *Background*: I'm working on an application that updates state in a database from an external feed. The process is: I read the feed, convert the data into SQLAlchemy objects which are detached from the session and then call sessio

Re: [sqlalchemy] SQLAlchemy Access to Native Bulk Loaders

2020-04-19 Thread James Fennell
Hi Ben, Have you checked out bulk operations? https://docs.sqlalchemy.org/en/13/orm/persistence_techniques.html#bulk-operations Bulk operations provide a DB-agnostic API for doing large numbers of inserts and/or updates. The speed up you see, compared to session.add, depends on your database and