[sqlalchemy] Postgresq Execute Many with Textual SQL Convenience Issue

2023-11-02 Thread mkmo...@gmail.com
Hi Mike, When using Core, we can do a bulk insert and bulk return with Postgresql trivially: from sqlalchemy import table, column t = table('foo', column('id'), column('bar') ins = t.insert().values([{'bar': 'a'}, {'bar': 'b'}]).returning(foo.id) results = conn.execute(ins)

Re: [sqlalchemy] Issuing Raw SQL and Returning a List of Objects

2023-11-02 Thread mkmo...@gmail.com
Hi Mike, If I understand correctly, you want to work with raw sql and don't want any ORM getting in your way. I'm the same way, and it is trivial to use SQLAlchemy Core for this purpose. results = conn.execute(text('select foo, bar from baz')).mappings().fetchall() # mappings().fetchall()

Re: [sqlalchemy] row.Row and dict behavior in 2.0

2023-01-13 Thread mkmo...@gmail.com
Hi Mike, That's much more concise, thank you. Previously I have always accessed the `_asdict()` private member. Thanks and best regards, Matthew On Thursday, January 12, 2023 at 11:00:58 AM UTC-8 Mike Bayer wrote: > > > On Thu, Jan 12, 2023, at 1:46 PM, mkmo...@gmail.com wrote: &g

Re: [sqlalchemy] row.Row and dict behavior in 2.0

2023-01-12 Thread mkmo...@gmail.com
()) something else? Thanks and best regards, Matthew On Monday, January 9, 2023 at 6:00:08 PM UTC-8 Mike Bayer wrote: > > > On Mon, Jan 9, 2023, at 8:50 PM, mkmo...@gmail.com wrote: > > Hello, > > It looks like in 2.0 we can no longer treat a row.Row as a dict. I have a

[sqlalchemy] row.Row and dict behavior in 2.0

2023-01-09 Thread mkmo...@gmail.com
Hello, It looks like in 2.0 we can no longer treat a row.Row as a dict. I have a few cases where I want to do this, such as when I need to get a list of columns, or when I don't know the column name in advance. rows = conn.execute(select(t.c.foo)).fetchall() rows[0].keys() # Not

[sqlalchemy] Does SQLAlchemy Core support MERGE?

2022-12-11 Thread mkmo...@gmail.com
Apologies if this is in the documentation, but I was not able to find it. Does SQLAlchemy Core support the MERGE statement? Thanks and best regards, Matthew -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide

[sqlalchemy] How to Dispose of all Connections in a Pool in a Web Environment?

2022-11-11 Thread mkmo...@gmail.com
I'm using the standard connection pool via engine: engine = sqlalchemy.create_engine('oracle://scott:tiger@foo', ...) conn1 = engine.connect() conn2 = engine.connect() I want to dispose and recreate the pool in a web environment. All new threads that connect should get a new

Re: [sqlalchemy] SELECT .. BULK COLLECT INTO ...

2022-10-05 Thread mkmo...@gmail.com
if match: > text = re.sub(r"BULK COLLECT INTO (.*)", "", text) > text = text.replace("FROM", f"BULK COLLECT INTO {match.group(1)} > FROM") > return text > > baz = table('baz', column('foo'), column('bar')) > > stmt =

[sqlalchemy] SELECT .. BULK COLLECT INTO ...

2022-10-05 Thread mkmo...@gmail.com
Hello, I am generating Oracle PLSQL using sqlalchemy core. I would like to generate the following output: SELECT foo, bar BULK COLLECT INTO l_foos FROM baz; Is there a mechanism to insert a string after the select column list and before the FROM? In sql/compiler.py:visit_select

Re: [sqlalchemy] Possible to pass array of (table, conditions) to join() like where()/select() ?

2022-09-17 Thread mkmo...@gmail.com
Thanks! On Friday, September 16, 2022 at 10:53:59 AM UTC-7 Mike Bayer wrote: > > > On Fri, Sep 16, 2022, at 12:10 PM, mkmo...@gmail.com wrote: > > I use the following pattern in my REST APIs, building up the select, > joins, where conditions, group bys, order bys, depe

[sqlalchemy] Possible to pass array of (table, conditions) to join() like where()/select() ?

2022-09-16 Thread mkmo...@gmail.com
I use the following pattern in my REST APIs, building up the select, joins, where conditions, group bys, order bys, depending on the query parameters passed in by the user: selects = [Foo] joins = [(Bar, Foo.c.id == Bar.c.foo_id)] where_conditions = [Foo.c.id == request.args['pk']]

[sqlalchemy] How to Warm Up Connection Pool ?

2022-08-23 Thread mkmo...@gmail.com
As stated in the docs: > All SQLAlchemy pool implementations have in common that none of them “pre create” connections - all implementations wait until first use before creating a connection Is it possible to warm up, or pre create, the connections in the pool? When I start my server, the

[sqlalchemy] Re: How to Prevent SQLAlchemy Core from Quoting Name

2022-06-17 Thread mkmo...@gmail.com
My apologies, I just saw that I can use the schema key word for this case: t = table('mytable', column('id'), schema='myschema') Best regards, Matthew On Friday, June 17, 2022 at 11:13:44 AM UTC-7 mkmo...@gmail.com wrote: > Using a `.` in a table name will cause the table n

[sqlalchemy] How to Prevent SQLAlchemy Core from Quoting Name

2022-06-17 Thread mkmo...@gmail.com
Using a `.` in a table name will cause the table name to be quoted. For example: from sqlalchemy import table, column t = table('myschema.mytable', column('id')) print(select(t.c.id)) Outputs: SELECT "myschema.mytable".id FROM "myschema.mytable" This fails in Oracle

Re: [sqlalchemy] How to infer the ORM model class from an update()/insert() object?

2022-03-28 Thread mkmo...@gmail.com
hub.com/sqlalchemy/sqlalchemy/issues/7865 > > this will allow retunring(User) to send back an instance but there's a lot > of cases to be worked out. > > > > > On Mon, Mar 28, 2022, at 2:05 PM, Mike Bayer wrote: > > > > On Mon, Mar 28, 2022, at 1:31 PM

Re: [sqlalchemy] How to infer the ORM model class from an update()/insert() object?

2022-03-28 Thread mkmo...@gmail.com
print(user.full_name) Thanks and best regards, Matthew On Sunday, March 27, 2022 at 7:28:55 PM UTC-7 Mike Bayer wrote: > > > On Sun, Mar 27, 2022, at 2:56 PM, mkmo...@gmail.com wrote: > > Hi Mike, > > I'm writing a library that uses SQLAlchemy. The user will pass the librar

Re: [sqlalchemy] How to infer the ORM model class from an update()/insert() object?

2022-03-27 Thread mkmo...@gmail.com
. Is there a public API I can use to take a Core `row` and convert it to an ORM model? Thanks and best regards, Matthew On Sunday, March 27, 2022 at 11:11:30 AM UTC-7 Mike Bayer wrote: > > > On Sun, Mar 27, 2022, at 2:08 PM, mkmo...@gmail.com wrote: > > Hi Mike, > >

Re: [sqlalchemy] How to infer the ORM model class from an update()/insert() object?

2022-03-27 Thread mkmo...@gmail.com
ring) > > > upd = update(A) > > print(select(upd.table).column_descriptions) > > i might take a crack at cleaning this up now but the above will get you > what you need. > > On Sat, Mar 26, 2022, at 1:34 PM, mkmo...@gmail.com wrote: > > Hello, > > How c

[sqlalchemy] How to infer the ORM model class from an update()/insert() object?

2022-03-26 Thread mkmo...@gmail.com
Hello, How can I infer the ORM model class from an update (or insert, or delete) function result? upd = update(Project).values(name='foo').where( Project.id == 1 ) def my_library_function(session, upd): result = session.execute(upd) # how to get the Project ORM model here, using

Re: [sqlalchemy] Conditional Insertion of Single Row with Bind Parameters

2022-03-24 Thread mkmo...@gmail.com
, or list of dict/tuple, similar to the insert(Project).values API. Any thoughts? Best regards, Matthew On Wednesday, March 23, 2022 at 6:21:34 AM UTC-7 Mike Bayer wrote: > > > On Tue, Mar 22, 2022, at 2:46 PM, mkmo...@gmail.com wrote: > > I would like to do a conditional insert of

[sqlalchemy] Conditional Insertion of Single Row with Bind Parameters

2022-03-22 Thread mkmo...@gmail.com
I would like to do a conditional insert of a a single row. This is often useful in a CRUD app for checking permissions and inserting in a single database call: INSERT INTO project (name, user_id) SELECT :name, :user_id WHERE EXISTS ( SELECT 1 FROM users WHERE id = :user_id

[sqlalchemy] How to derive table/table name from a column.label()'d object?

2022-01-28 Thread mkmo...@gmail.com
In sqlalchemy Core, I can get the table and table name from a regular column by calling `c.table.name`: from sqlalchemy import table, column t = table('foo', column('bar')) assert hasattr(t.c.bar, 'table') print(t.c.bar.table.name) However, If I label a column, the label no

Re: [sqlalchemy] Automatically add Index on same columns in ForeignKeyConstraint in Postgresql using Custom DDL/Compilation

2021-06-25 Thread mkmo...@gmail.com
wrote: > > > On Fri, Jun 25, 2021, at 11:58 AM, mkmo...@gmail.com wrote: > > > When using PostgreSQL, creating a foreign key on a column in a table does > not automatically index that column, unlike Oracle or MySQL. > > I would like to get SQLAlchemy to automatically creat

[sqlalchemy] Automatically add Index on same columns in ForeignKeyConstraint in Postgresql using Custom DDL/Compilation

2021-06-25 Thread mkmo...@gmail.com
When using PostgreSQL, creating a foreign key on a column in a table does not automatically index that column, unlike Oracle or MySQL. I would like to get SQLAlchemy to automatically create an index on the same columns that are specified in a ForeingKeyConstraint. For example, if I have a

Re: [sqlalchemy] Rationale for why 1.4 Core `select` argument as list is deprecated?

2021-05-13 Thread mkmo...@gmail.com
> typing, we still have to support it :) . It has no impact on you as a > user of the library, you can ignore typing completely and there's no issue > with that. > > > On Thu, May 13, 2021, at 9:55 AM, mkmo...@gmail.com wrote: > > Hi Mike. > > Thanks, it make

Re: [sqlalchemy] Rationale for why 1.4 Core `select` argument as list is deprecated?

2021-05-13 Thread mkmo...@gmail.com
SQLAlchemy 2.0 seeks to finally remove.As this API is one of the > most complicated to provide a backwards-compatibility path for, it's taken > a very long time for us to get there.But here we are. > > > > > On Thu, May 13, 2021, at 2:10 AM, mkmo...@gmail.com wrot

[sqlalchemy] Rationale for why 1.4 Core `select` argument as list is deprecated?

2021-05-13 Thread mkmo...@gmail.com
Hello, I am on Sqlalchemy 1.2 and looking to upgrade to 1.4. I have a lot of Core code that looks like this: # returns an array of columns select_fields = make_select_fields(...) sel = select(select_fields) Passing an array to select is now deprecated, so I'm planning on changing

Re: [sqlalchemy] Using SQLAlchemy Core as Query Builder, not ORM, in CRUD applications

2021-04-23 Thread mkmo...@gmail.com
that I have > not really covered. e.g. If I pass in a tuple if (‘tableName’, > sessionObject) then the code will do the insert/update inside a transaction > for that session, rather than a direct update etc. > > Hope you gain some insight and inspiration. Happy to chat 1:1 if

[sqlalchemy] Using SQLAlchemy Core as Query Builder, not ORM, in CRUD applications

2021-04-22 Thread mkmo...@gmail.com
Hi All, Some people don't like ORMs and instead use query builders like SQLAlchemy Core (or even raw SQL text) even for something like a CRUD application. For sake of argument, let's put aside whether or not this is a good idea. I'm just curious how one would go about this without copy and