Re: [sqlalchemy] Differences between TableClause insert and Model.__table__ inserts?

2020-11-25 Thread Mike Bayer
technically Table and TableClause are a little bit different but I don't think 
there's any behavioral difference at the level of execute(obj.insert()).   what 
matters more is if the Column objects have datatypes or defaults that incur 
some kind of Python-side processing or not.

On Wed, Nov 25, 2020, at 4:33 PM, 'Jonathan Vanasco' via sqlalchemy wrote:
> This was not clear enough in Mike's post: `Foo.__table__` is the same type of 
> object as `_foo = table(...)`.  SQLAlchemy ORM is built on top of 
> SQLAlchemy's Core, so the  ORM's `.__table__` attribute is the Core's 
> `table()` object.
> 
> Since they're the same, the two will have the same performance within 
> `conn.execute(`.
> 
> On Wednesday, November 25, 2020 at 4:18:46 PM UTC-5 Kata Char wrote:
>> I see, does that mean there is no difference in performance if one or the 
>> other is used? In other words
>> from sqlalchemy.sql import table
>> 
>> _foo = table(...)
>> conn.execute(_foo.insert(), [{...}, ...])
>> 
>> Would have the same performance as `conn.execute(Foo.__table__.insert(), 
>> [{...}, ...])`
>> 
>> On Wednesday, November 25, 2020 at 8:27:53 AM UTC-8 Mike Bayer wrote:
>>> 
>>> 
>>> On Wed, Nov 25, 2020, at 10:30 AM, Kata Char wrote:
 Hi, sorry if this post is a duplicate, my first one didn't seem to make it.
 
 I was reading the documentation:
 - https://docs.sqlalchemy.org/en/13/core/tutorial.html#execute-multiple
 
 - 
 https://docs.sqlalchemy.org/en/13/_modules/examples/performance/bulk_inserts.html
 
 Is there any difference between conn.execute(TableClause.insert(), [...]) 
 vs conn.execute(Model.__table__.insert(), [...])?
 
 The first one is documented to use execumany(), but what about the second 
 one? 
>>> 
>>> Any conn.execute() that passes a list of dictionaries as the second 
>>> argument, where there is more than one entry in the list, will use the 
>>> executemany() style with the DBAPI connection.
>>> 
>>> With the ORM the Model.__table__ attribute is a Table object.  That 
>>> tutorial seems to be referencing TableClause which is the base class for 
>>> Table, but all the examples there are using Table objects.
>>> 
>>> 
 

 -- 
 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+...@googlegroups.com.
 To view this discussion on the web visit 
 https://groups.google.com/d/msgid/sqlalchemy/1ffe48c6-4124-40ab-902f-ffa86885ea94n%40googlegroups.com
  
 .
>>> 
> 

> -- 
> 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/89002280-96e7-45e9-a11a-f104d8e2aa3fn%40googlegroups.com
>  
> .

-- 
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/3d5cf9e9-ce2e-4ded-9c04-3e46d2fbc0ff%40www.fastmail.com.


Re: [sqlalchemy] Differences between TableClause insert and Model.__table__ inserts?

2020-11-25 Thread 'Jonathan Vanasco' via sqlalchemy
This was not clear enough in Mike's post: `Foo.__table__` is the same type 
of object as `_foo = table(...)`.  SQLAlchemy ORM is built on top of 
SQLAlchemy's Core, so the  ORM's `.__table__` attribute is the Core's 
`table()` object.

Since they're the same, the two will have the same performance within 
`conn.execute(`.

On Wednesday, November 25, 2020 at 4:18:46 PM UTC-5 Kata Char wrote:

> I see, does that mean there is no difference in performance if one or the 
> other is used? In other words
> from sqlalchemy.sql import table
>
> _foo = table(...)
> conn.execute(_foo.insert(), [{...}, ...])
>
> Would have the same performance as `conn.execute(Foo.__table__.insert(), 
> [{...}, ...])`
>
> On Wednesday, November 25, 2020 at 8:27:53 AM UTC-8 Mike Bayer wrote:
>
>>
>>
>> On Wed, Nov 25, 2020, at 10:30 AM, Kata Char wrote:
>>
>> Hi, sorry if this post is a duplicate, my first one didn't seem to make 
>> it.
>>
>> I was reading the documentation:
>> - https://docs.sqlalchemy.org/en/13/core/tutorial.html#execute-multiple
>>
>> - 
>> https://docs.sqlalchemy.org/en/13/_modules/examples/performance/bulk_inserts.html
>>
>> Is there any difference between conn.execute(TableClause.insert(), [...]) 
>> vs conn.execute(Model.__table__.insert(), [...])?
>>
>> The first one is documented to use execumany(), but what about the second 
>> one? 
>>
>>
>> Any conn.execute() that passes a list of dictionaries as the second 
>> argument, where there is more than one entry in the list, will use the 
>> executemany() style with the DBAPI connection.
>>
>> With the ORM the Model.__table__ attribute is a Table object.  That 
>> tutorial seems to be referencing TableClause which is the base class for 
>> Table, but all the examples there are using Table objects.
>>
>>
>>
>> -- 
>> 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+...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/sqlalchemy/1ffe48c6-4124-40ab-902f-ffa86885ea94n%40googlegroups.com
>>  
>> 
>> .
>>
>>
>>

-- 
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/89002280-96e7-45e9-a11a-f104d8e2aa3fn%40googlegroups.com.


Re: [sqlalchemy] Differences between TableClause insert and Model.__table__ inserts?

2020-11-25 Thread Kata Char
I see, does that mean there is no difference in performance if one or the 
other is used? In other words
from sqlalchemy.sql import table

_foo = table(...)
conn.execute(_foo.insert(), [{...}, ...])

Would have the same performance as `conn.execute(Foo.__table__.insert(), 
[{...}, ...])`

On Wednesday, November 25, 2020 at 8:27:53 AM UTC-8 Mike Bayer wrote:

>
>
> On Wed, Nov 25, 2020, at 10:30 AM, Kata Char wrote:
>
> Hi, sorry if this post is a duplicate, my first one didn't seem to make it.
>
> I was reading the documentation:
> - https://docs.sqlalchemy.org/en/13/core/tutorial.html#execute-multiple
>
> - 
> https://docs.sqlalchemy.org/en/13/_modules/examples/performance/bulk_inserts.html
>
> Is there any difference between conn.execute(TableClause.insert(), [...]) 
> vs conn.execute(Model.__table__.insert(), [...])?
>
> The first one is documented to use execumany(), but what about the second 
> one? 
>
>
> Any conn.execute() that passes a list of dictionaries as the second 
> argument, where there is more than one entry in the list, will use the 
> executemany() style with the DBAPI connection.
>
> With the ORM the Model.__table__ attribute is a Table object.  That 
> tutorial seems to be referencing TableClause which is the base class for 
> Table, but all the examples there are using Table objects.
>
>
>
> -- 
> 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+...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/1ffe48c6-4124-40ab-902f-ffa86885ea94n%40googlegroups.com
>  
> 
> .
>
>
>

-- 
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/ea6c2037-613c-4ebf-a4d3-e6fba46cf743n%40googlegroups.com.


[sqlalchemy] Re: Dis/Reassociate objects with a db session.

2020-11-25 Thread 'Jonathan Vanasco' via sqlalchemy
Read the docs on State Management and pay attention to `merge`:

   https://docs.sqlalchemy.org/en/14/orm/session_state_management.html

Also, to simplify this stuff a popular related pattern is to use  a 
RevisionID or RevisionTimestamp on the objects.  In the first session, you 
note the version information. On the second session you fetch a new object 
and ensure it is the same - if so, your data is safe to update.  If not, 
the objects became out of-sync and may require more logic.



On Wednesday, November 25, 2020 at 12:57:23 PM UTC-5 jens.t...@gmail.com 
wrote:

> Hello,
>
> My question is regarding long-running tasks and db sessions. Currently I 
> have the very rare situation where a task takes longer than a db session is 
> valid and thus fails when it wants to write back results. Extending the TTL 
> of a db session is probably not a good idea.
>
> I think the proper approach would be to open a db session, fetch data, 
> close the db session, do work, open a new db session, write data, close the 
> db session. So, I must make sure that I fetch all data ahead of time while 
> the first session is active.
>
> Is there a way to re-associate objects that belonged to the first session 
> with a newly opened one? What’s the recommended approach here, does SQLA 
> have any magic in store to help me with very long-lived ORM objects across 
> db sessions? Or should I manage that data independently of their respective 
> ORM objects?
>
> Thanks!
> Jens
>

-- 
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/bf1d5c69-d500-4cac-bb29-026f1343a37bn%40googlegroups.com.


[sqlalchemy] Dis/Reassociate objects with a db session.

2020-11-25 Thread jens.t...@gmail.com
Hello,

My question is regarding long-running tasks and db sessions. Currently I 
have the very rare situation where a task takes longer than a db session is 
valid and thus fails when it wants to write back results. Extending the TTL 
of a db session is probably not a good idea.

I think the proper approach would be to open a db session, fetch data, 
close the db session, do work, open a new db session, write data, close the 
db session. So, I must make sure that I fetch all data ahead of time while 
the first session is active.

Is there a way to re-associate objects that belonged to the first session 
with a newly opened one? What’s the recommended approach here, does SQLA 
have any magic in store to help me with very long-lived ORM objects across 
db sessions? Or should I manage that data independently of their respective 
ORM objects?

Thanks!
Jens

-- 
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/96e71deb-b996-4695-8527-5a3b5bceff45n%40googlegroups.com.


Re: [sqlalchemy] Differences between TableClause insert and Model.__table__ inserts?

2020-11-25 Thread Mike Bayer


On Wed, Nov 25, 2020, at 10:30 AM, Kata Char wrote:
> Hi, sorry if this post is a duplicate, my first one didn't seem to make it.
> 
> I was reading the documentation:
> - https://docs.sqlalchemy.org/en/13/core/tutorial.html#execute-multiple
> 
> - 
> https://docs.sqlalchemy.org/en/13/_modules/examples/performance/bulk_inserts.html
> 
> Is there any difference between conn.execute(TableClause.insert(), [...]) vs 
> conn.execute(Model.__table__.insert(), [...])?
> 
> The first one is documented to use execumany(), but what about the second 
> one? 

Any conn.execute() that passes a list of dictionaries as the second argument, 
where there is more than one entry in the list, will use the executemany() 
style with the DBAPI connection.

With the ORM the Model.__table__ attribute is a Table object.  That tutorial 
seems to be referencing TableClause which is the base class for Table, but all 
the examples there are using Table objects.


> 

> -- 
> 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/1ffe48c6-4124-40ab-902f-ffa86885ea94n%40googlegroups.com
>  
> .

-- 
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/2019f1cc-3e29-4b24-b1d5-180788c83d8a%40www.fastmail.com.


[sqlalchemy] Differences between TableClause insert and Model.__table__ inserts?

2020-11-25 Thread Kata Char
Hi, sorry if this post is a duplicate, my first one didn't seem to make it.

I was reading the documentation:
- https://docs.sqlalchemy.org/en/13/core/tutorial.html#execute-multiple

- 
https://docs.sqlalchemy.org/en/13/_modules/examples/performance/bulk_inserts.html

Is there any difference between conn.execute(TableClause.insert(), [...]) 
vs conn.execute(Model.__table__.insert(), [...])?

The first one is documented to use execumany(), but what about the second 
one? 

-- 
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/1ffe48c6-4124-40ab-902f-ffa86885ea94n%40googlegroups.com.