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

2022-03-27 Thread Mike Bayer
emy.org/c/sqlalchemy/sqlalchemy/+/3742 > > In my case, `type`, `expr` and `entity` all return the model class that I am > interested in. > > Thanks and best regards, > > Matthew > On Saturday, March 26, 2022 at 12:02:54 PM UTC-7 Mike Bayer wrote: >&

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

2022-03-26 Thread Mike Bayer
the Project model is actually in there, but not in a public API place (this is not the solution, but look inside of table._annotations to see it). The closest public API we have for this very new API right now is the Query equivalent of column_descriptions, which is available on the select()

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

2022-03-23 Thread Mike Bayer
On Tue, Mar 22, 2022, at 2:46 PM, mkmo...@gmail.com wrote: > 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 >

Re: [sqlalchemy] Multiple Database Connection

2022-03-22 Thread Mike Bayer
ng entirely different, but you haven't specified this. On Tue, Mar 22, 2022, at 10:26 AM, Mustafa Demiraslan wrote: > Current structure is like this: > > image.png > > I can't manage pooling for green colored databases. > > > Mike Bayer , 22 Mar 2022 Sal, 17:12 ta

Re: [sqlalchemy] Multiple Database Connection

2022-03-22 Thread Mike Bayer
for one engine with 18 databases, what is "switching the bindings" ? do you mean you call "USE " on the connection ? creating 18 separate engines would not be that big a deal if that's what works best for the code, no issue doing that. On Tue, Mar 22, 2022, at 9:41 AM, Mustafa Demiraslan

Re: [sqlalchemy] Caching with selectedinload causing AttributeError: 'PathToken' object has no attribute 'serialize'

2022-03-19 Thread Mike Bayer
that is an immensely complicated query and I couldn't tell you what's going on with that being the only context provided, unfortunately. On Fri, Mar 18, 2022, at 10:16 PM, Michael Bukachi wrote: > Hello, > > So I've got a very weird when trying to set up caching using dogpile based on > the

Re: [sqlalchemy] None! Can't pickle : it's not the same object as sqlalchemy.orm.session.Session

2022-03-16 Thread Mike Bayer
can confirm, don't try passing Session objects as arguments over multiprocessing. that's not going to work for many reasons. new child process should use its own session. On Wed, Mar 16, 2022, at 6:42 AM, Simon King wrote: > I haven't used the multiprocessing library, but if it uses pickle

Re: [sqlalchemy] Re: many-to-many orm warnings

2022-03-10 Thread Mike Bayer
hey there. The warnings go away entirely by making Parent.children viewonly=True, which for this type of mapping is recommended: class Parent(Base): __tablename__ = "left" id = Column(Integer, primary_key=True) children = relationship( "Child",

Re: [sqlalchemy] Async Event Listeners

2022-02-24 Thread Mike Bayer
the error seems like what would happen right now, sure. we have a complete doc section now on setting up event handlers with async objects: https://docs.sqlalchemy.org/en/14/orm/extensions/asyncio.html#using-events-with-the-asyncio-extension On Thu, Feb 24, 2022, at 2:56 PM, Brendan

Re: [sqlalchemy] Copy a dataframe to postgresql database within a schema

2022-02-23 Thread Mike Bayer
the "from msilib import schema" import is at the top of your script. it looks like it's there by accident. are you using vscode? I find it often adds random package names as it guesses from my typing. I'd remove that line. On Wed, Feb 23, 2022, at 1:54 PM, Nahum Castro wrote: > Hello All. >

Re: [sqlalchemy] CTE w/VALUES in SELECT?

2022-02-21 Thread Mike Bayer
rect? Any way to do this > under 1.3? > > > On Feb 20, 2022, at 8:17 PM, Mike Bayer wrote: > > > > the Values construct doesn't have CTE direct support right now so you need > > to make a subquery first, then CTE from that > > > > from sqlalchemy i

Re: [sqlalchemy] CTE w/VALUES in SELECT?

2022-02-20 Thread Mike Bayer
the Values construct doesn't have CTE direct support right now so you need to make a subquery first, then CTE from that from sqlalchemy import Column from sqlalchemy import column from sqlalchemy import Integer from sqlalchemy import select from sqlalchemy import String from sqlalchemy import

Re: [sqlalchemy] garbage collection of instances() generator causing psycopg2.ProgrammingError: named cursor isn't valid anymore

2022-02-16 Thread Mike Bayer
uot;{i}") for i in range(300)]) s.commit() # SS cursor, will fetch every 10 rows x = iter(s.query(A).yield_per(10)) for i in range(157): o = next(x) s.commit() del x import gc gc.collect() print("done") On Wed, Feb 16, 2022, at 6:14 PM, Mike Bayer wrote: > hi there - > >

Re: [sqlalchemy] garbage collection of instances() generator causing psycopg2.ProgrammingError: named cursor isn't valid anymore

2022-02-16 Thread Mike Bayer
hi there - this issue does not sound familiar , I guess you are trying to remove the Query iterator before it's exhausted and therefore problems are occurring. I would need to see a stack trace to understand the issue. Also, you shouldn't use Query for cases like these, use modern 1.4 style

Re: [sqlalchemy] Self-referential relationship with multiple intermediary tables

2022-02-14 Thread Mike Bayer
I'm not able to reproduce with SQLAlhcemy 1.4. The first join condition given works and if I print a query as follows: s = Session() acc_alias = aliased(Account) print( s.query(Account).join(Account.downstream_accounts.of_type(acc_alias)) ) SQL output is: SELECT account.id AS account_id

Re: [sqlalchemy] password rotation after engine created

2022-02-04 Thread Mike Bayer
way I can catch > "ORA-01017: invalid username/password; logon denied" and try password update > instead of every time calling secret client to fetch same password. > > Regards, > Srinu > > On Friday, February 4, 2022 at 8:56:49 AM UTC-8 Mike Bayer wrote: >

Re: [sqlalchemy] password rotation after engine created

2022-02-04 Thread Mike Bayer
we have a recipe for this scheme here: https://docs.sqlalchemy.org/en/14/core/engines.html#generating-dynamic-authentication-tokens no need to connect() inside the event handler, just update the parameters with the new password. On Fri, Feb 4, 2022, at 11:14 AM, Srinu Chp wrote: > Hello

Re: [sqlalchemy] One-to-one relationship not behaving as expected when trying to replace object

2022-02-04 Thread Mike Bayer
ll > out than it would to simply replace the object with a new instance. > > In that case, what would you suggest as a workaround? Would it be to do an > explicit session.flush() before assigning the new instance to the > relationship? > > On Thursday,

Re: [sqlalchemy] Mapping column names

2022-02-03 Thread Mike Bayer
the names of attributes on your Python class and the names of columns that are emitted in SQL are two separate things.When you have "jobid = Column(Integer, ...)" , that's a declarative-only format that omits the first argument to Column which is the "name"; the declarative mapping process

Re: [sqlalchemy] One-to-one relationship not behaving as expected when trying to replace object

2022-02-03 Thread Mike Bayer
this is the long-expected behavior of the unit of work when issuing a delete() and then an add() of two different objects that nonetheless have the same primary key value - instead of DELETE and INSERT, you get an UPDATE. the reasons have to do with the unit-of-work's ordering of

Re: [sqlalchemy] PostgreSQL UUID type, Python’s UUID — and type hints

2022-01-24 Thread Mike Bayer
> and in some cases mypy is still able to infer the correct type. In this case, SQLAlchemy's mypy plugin does the inference in this case based on hardcoded rules that inspect the Column objects. > > Much thanks, > Jens > > > On Monday, January 17, 2022 at 1:04:4

Re: [sqlalchemy] PostgreSQL UUID type, Python’s UUID — and type hints

2022-01-16 Thread Mike Bayer
of Mapped[User] or User) when using the Mypy plugin. > > Other than following the migration guide > <https://docs.sqlalchemy.org/en/14/changelog/migration_20.html>, I haven’t > looked into SQLA2 <https://github.com/sqlalchemy/sqlalchemy/projects/3>. > Jens > >

Re: [sqlalchemy] PostgreSQL UUID type, Python’s UUID — and type hints

2022-01-13 Thread Mike Bayer
is this with the SQLAlchemy Mypy plugin? current status is for 2.0 we are looking to move away from the plugin model and pretty much change how these things work.Otherwise if this is with the plugin, you would use "id: Mapped[uuid.UUID] = ..." On Wed, Jan 12, 2022, at 9:35 PM,

Re: [sqlalchemy] Debugging memory leaks

2022-01-12 Thread Mike Bayer
eate, use, and destroy them? > How do we identify if they are continuously created and not destroyed? > On Thursday, January 6, 2022 at 5:28:00 AM UTC-8 Mike Bayer wrote: >> __ >> 1.3.16 doesn't have a lot of caching going on compared to the 1.4 series. >> it's expected

Re: [sqlalchemy] Turn off connections invalidation functionality

2022-01-10 Thread Mike Bayer
;> >> On Thursday, January 6, 2022 at 5:23:09 AM UTC-8 Mike Bayer wrote: >>> __ >>> I can't guarantee that overriding private methods is safe, no. >>> >>> there's a public API to disable errors resulting in invalidation, I suggest >>>

Re: [sqlalchemy] With loader criteria and baked query cache

2022-01-07 Thread Mike Bayer
i dont think with_loader_criteria ever expected the target entity to be part of "secondary" in a relationship(). I have no idea what that would do and I'm surprised it works at all. I would try instead to map from A->A_rel_B->B explicitly and use two relationships for this join. On Fri,

Re: [sqlalchemy] [Question] Why not passing Connection URL query parameters to the dialect?

2022-01-07 Thread Mike Bayer
a few which usually refers to some SQL generation behaviors, but most of these are themselves derived from inspecting the database itself, after the connection has been established. On Fri, Jan 7, 2022, at 11:00 AM, Mike Bayer wrote: > the idea of Table objects being linked to a datab

Re: [sqlalchemy] [Question] Why not passing Connection URL query parameters to the dialect?

2022-01-07 Thread Mike Bayer
ther bits in the table definition needs to change too, like > the name of the schema. So there is no real benefit and one has to create > several Table objects anyway. > And the use of the connection is just an unfortunate hack... And this is an > issue that should be

Re: [sqlalchemy] [Question] Why not passing Connection URL query parameters to the dialect?

2022-01-06 Thread Mike Bayer
hey there - database URLs do support query string parameters, however they have a specific meaning which is that they are consumed by the DBAPI in use, not the dialect directly. Please review the docs at

Re: [sqlalchemy] Debugging memory leaks

2022-01-06 Thread Mike Bayer
1.3.16 doesn't have a lot of caching going on compared to the 1.4 series. it's expected that memory in an ORM application will grow for a few iterations as there is some caching of things, but a "leak" is defined as growth that never stops. you'd want to verify that the growth you see does

Re: [sqlalchemy] Turn off connections invalidation functionality

2022-01-06 Thread Mike Bayer
gt; Great, thanks for the info. >> >> On Friday, November 26, 2021 at 9:30:02 AM UTC-8 Mike Bayer wrote: >>> __ >>> I've spent some time thinking about what might be being asked here.the >>> only thing I can think of is that when a particular databas

Re: [sqlalchemy] Best practices for sub-dividing a large collection relationship

2021-12-31 Thread Mike Bayer
ewonly=True relationship) without requiring an > additional flush at an a point in time when one is not allowed? > > Note that I have also tried to accomplish this using "after_flush" and > "after_flush_postexec" event listeners, but the problem with these is I don'

Re: [sqlalchemy] Best practices for sub-dividing a large collection relationship

2021-12-30 Thread Mike Bayer
On Thu, Dec 30, 2021, at 8:10 PM, Dane K Barney wrote: > Given the following example model: > > class Node(Base): > __tablename__ = "node" > id = Column(Integer, primary_key=True, autoincrement=True) > > class Edge(Base): > __tablename__ = "edge" > id = Column(Integer,

Re: [sqlalchemy] Setting up indexes in __table_args__ that depend on columns created in __declare_first__

2021-12-14 Thread Mike Bayer
Hi Randy! inside of __declare_first__() the table is ready, you can just set it up def __declare_first__(cls): Index("my_idx", cls.__table__.c.colname) have a great holiday season! - mike On Tue, Dec 14, 2021, at 4:32 PM, 'Randy Syring' via sqlalchemy wrote: > I'm trying to create a

Re: [sqlalchemy] How to SELECT computed data using SQLAlchemy?

2021-12-14 Thread Mike Bayer
.label("my_label")])) SELECT jc_tanimoto(:jc_tanimoto_1) AS my_label so as long as there's no unusual SQL syntaxes in play you can use func. to generate SQL for any SQL function with parameters. > > Thanks again, > Dan > > On Tuesday, 14 December 2021

Re: [sqlalchemy] How to SELECT computed data using SQLAlchemy?

2021-12-14 Thread Mike Bayer
On Tue, Dec 14, 2021, at 5:40 AM, 'Daniel Grindrod' via sqlalchemy wrote: > Hi all, > > I'm working on a REST API which is built using Flask-SQLAlchemy and > Connexion. I'm fairly new to SQLAlchemy, but it's been brilliant so far :) > This API uses SQLAlchemy 1.3.16, and connects to an Oracle

Re: [sqlalchemy] Not able to importing fixtures from sqlalchemy from 1.3.16 version

2021-12-12 Thread Mike Bayer
.17. > > Thank you. > Sai Rohith Yerramilli > On Saturday, December 11, 2021 at 10:17:15 PM UTC+5:30 Mike Bayer wrote: >> __ >> not expected to work right now unless you run pytest and you have the >> appropriate project layout installed as documented at >&

Re: [sqlalchemy] Not able to importing fixtures from sqlalchemy from 1.3.16 version

2021-12-11 Thread Mike Bayer
of code is working good in SQLAlchemy 1.3.15 version and below > but failing in above versions. > Getting issue when importing "from sqlalchemy.testing import fixtures". > > Thank you. > > > On Thursday, December 9, 2021 at 11:00:58 PM UTC+5:30 Mike Bayer wr

Re: [sqlalchemy] Not able to importing fixtures from sqlalchemy from 1.3.16 version

2021-12-09 Thread Mike Bayer
these fixtures are only for use within a pytest test suite that is using SQLAlchemy's plugins. is this for a third party dialect project? On Thu, Dec 9, 2021, at 9:42 AM, sairohith yerramilli wrote: > Hi > I am facing an issue when importing fixtures from sqlalchemy in version > 1.3.16 and

Re: [sqlalchemy] session merge sets missing children foreign keys to null

2021-12-08 Thread Mike Bayer
On Wed, Dec 8, 2021, at 10:38 AM, Gabriel Smith wrote: > Hi, I'm a recent adopter of sqlalchemy, starting fresh with all the 2.0 > stuff. Thanks so much for the entire teams hard work! great! > > I have a small issue with the merge functionality and it could honestly just > be a

Re: [sqlalchemy] @declared_attr.cascading doesn't work when inheritance of multiple levels

2021-11-29 Thread Mike Bayer
n keys to each > classes. Not a big deal, actually it helps clear code! > On Sunday, November 28, 2021 at 6:37:30 PM UTC-8 Mike Bayer wrote: >> __ >> this is addressed in the docs which discuss "cascading" here: >> >> https://docs.sqlalchemy.org/en/14

Re: [sqlalchemy] @declared_attr.cascading doesn't work when inheritance of multiple levels

2021-11-28 Thread Mike Bayer
'programmer' > record_id = Column(ForeignKey('engineer.record_id'), >primary_key=True) > > On Sunday, November 28, 2021 at 8:25:30 AM UTC-8 Mike Bayer wrote: >> >> >> On Sun, Nov 28, 2021, at 4:24 AM, niuji...@gmail.com wrote: >>&

Re: [sqlalchemy] @declared_attr.cascading doesn't work when inheritance of multiple levels

2021-11-28 Thread Mike Bayer
On Sun, Nov 28, 2021, at 4:24 AM, niuji...@gmail.com wrote: > I'm using the "joined table inheritance" model. I have three levels of > inheritance. > > class has_polymorphic_id(object): > @declared_attr.cascading > def record_id(cls): > if has_inherited_table(cls): >

Re: [sqlalchemy] When building a query, option to only join once

2021-11-27 Thread Mike Bayer
well as the email noted, it referred to issue https://github.com/sqlalchemy/sqlalchemy/issues/3225 , which was an ambitious proposal for a richly featured inspection API on the Query object. which would mean you'd still need to write your own logic that pokes around in the query, decides

Re: [sqlalchemy] Turn off connections invalidation functionality

2021-11-26 Thread Mike Bayer
e_pool_on_disconnect#sqlalchemy.engine.ExceptionContext.invalidate_pool_on_disconnect . On Fri, Nov 26, 2021, at 11:51 AM, Mike Bayer wrote: > Im not sure if I understand the question? if you don't call .invalidate(), > then the connection is not invalidated. > > what does "turn off" mean ? > > > > On Fri, No

Re: [sqlalchemy] Turn off connections invalidation functionality

2021-11-26 Thread Mike Bayer
Im not sure if I understand the question? if you don't call .invalidate(), then the connection is not invalidated. what does "turn off" mean ? On Fri, Nov 26, 2021, at 11:17 AM, Anupama Goparaju wrote: > Hi, > > Is there a way to safely turn off connection invalidation functionality >

Re: [sqlalchemy] Using replacement_traverse() to augment a join

2021-11-26 Thread Mike Bayer
hey there - replacement_traverse is a mostly internal API that is very fickle and is not guaranteed to work in generic situations; it's tuned very specifically to cases that the ORM needs internally and one of the architectural trends in SQLAlchemy is to try to reduce dependence on it. Can we

Re: [sqlalchemy] sorting hybrid_properties getting NotImplementedError: Operator 'getitem' is not supported on this expression

2021-11-19 Thread Mike Bayer
there's no need to use custom Comparator objects, these are difficult to get right and there's not really any need to use them except in the extremely unusual case that you need specific SQL operators to do something different, which itself is not really a thing in modern SQLAlchemy. Just use

Re: [sqlalchemy] sorting hybrid_properties getting NotImplementedError: Operator 'getitem' is not supported on this expression

2021-11-19 Thread Mike Bayer
heya - haven't run it yet but if you are on SQLAlchemy 1.3, the signature for case() is different (sorry, note the list ): https://docs.sqlalchemy.org/en/13/core/sqlelement.html?highlight=case#sqlalchemy.sql.expression.case expr = case( [(cls.Type == "SELL", cast(cls.Units *

Re: [sqlalchemy] correlated subquery as column_property can't be queried independently?

2021-11-10 Thread Mike Bayer
ly > needed. Is there a parameter to return a series of scalar value like this? > On Wednesday, November 10, 2021 at 12:05:27 PM UTC-8 Mike Bayer wrote: >> __ >> it has to do with how SQLAlchemy determines the FROM list in a select() >> statement. >> >> if you say select

Re: [sqlalchemy] correlated subquery as column_property can't be queried independently?

2021-11-10 Thread Mike Bayer
t; learn it right. > > On Wednesday, November 10, 2021 at 5:55:44 AM UTC-8 Mike Bayer wrote: >> __ >> try calling: >> >> query(C.symbol_from_a).select_from(C) >> >> >> >> On Wed, Nov 10, 2021, at 4:50 AM, niuji...@gmail.com wrote: >>&

Re: [sqlalchemy] correlated subquery as column_property can't be queried independently?

2021-11-10 Thread Mike Bayer
try calling: query(C.symbol_from_a).select_from(C) On Wed, Nov 10, 2021, at 4:50 AM, niuji...@gmail.com wrote: > class A(Base): > primary_id = Column(Integer, prirmary_key=True) > some_A_marker = Column(String) > > class B(Base): > primary_id = Column(Integer, primary_key=True) >

Re: [sqlalchemy] Child class attributes not loading up in joined inheritance model

2021-11-09 Thread Mike Bayer
note we are following up w/ this question at discussions: https://github.com/sqlalchemy/sqlalchemy/discussions/7303 On Mon, Nov 8, 2021, at 6:07 AM, Shefeek Najeeb wrote: > Hi all, > > It has been only a while since I've started tinkering with SQLAlchemy. I aws > working on a particular

Re: [sqlalchemy] Many-to-many cascade delete

2021-11-02 Thread Mike Bayer
The answers regarding "cascade" are correct, however this question has been asked a few times before and we have a recipe on the Wiki that discusses this problem and presents an ORM event oriented solution, which is at https://github.com/sqlalchemy/sqlalchemy/wiki/ManyToManyOrphan , which I put

Re: [sqlalchemy] Returning Postgres JSON view as JSON string without serialization

2021-10-29 Thread Mike Bayer
oh also, if this is a view, much easier, just put the CAST to TEXT in your CREATE VIEW statement. that way you will definitely get strings back and nothing json related will kick in client side. On Fri, Oct 29, 2021, at 9:16 PM, Mike Bayer wrote: > psycopg2 driver (if that's what you

Re: [sqlalchemy] Returning Postgres JSON view as JSON string without serialization

2021-10-29 Thread Mike Bayer
psycopg2 driver (if that's what you're using) jumps in to do the JSON so if you dont want json you need to cast as a string, like cast(table.c.json_col, String). if you are using a Table with autoload you'd want to override this type using the technique detailed at

Re: [sqlalchemy] Is "uselist=False" imcompatible with lazy='joined' in relationship?

2021-10-22 Thread Mike Bayer
On Fri, Oct 22, 2021, at 6:45 AM, niuji...@gmail.com wrote: > Although the official documentation is very robust and detailed, one thing I > noticed is not very clear. > > When specifying arguments for a relationship like this: > > class Bonus(Base): > > basis =

Re: [sqlalchemy] Alembic: Logging issue after configuration change (EBv2)

2021-10-21 Thread Mike Bayer
On Thu, Oct 21, 2021, at 12:08 PM, cmana...@gmail.com wrote: > Hi, > > Like the title says, I'm seeing some weird logging issues in production. > Using alembic and flask-migrate latest. Works great locally (no issues) and > worked great on the server prior to a platform change. > > I think

Re: [sqlalchemy] Core insert and named columns

2021-10-20 Thread Mike Bayer
https://github.com/sqlalchemy/sqlalchemy/issues/7217 On Wed, Oct 20, 2021, at 1:01 PM, Mike Bayer wrote: > for the moment yes, because there is no ORM overlay for the insert() > construct.the case below should be improved however. > > reliable way is to use the attribute

Re: [sqlalchemy] Core insert and named columns

2021-10-20 Thread Mike Bayer
for the moment yes, because there is no ORM overlay for the insert() construct. the case below should be improved however. reliable way is to use the attributes: stmt = insert(A).values({A.id: 1, A.field: 3}) On Wed, Oct 20, 2021, at 6:31 AM, Tomas Pavlovsky wrote: > Hi, > > class A():

Re: [sqlalchemy] Dialect-specific dispatch in user code

2021-10-19 Thread Mike Bayer
On Tue, Oct 19, 2021, at 1:41 PM, Jonathan Brandmeyer wrote: > We're supporting both Postgres and SQLite in our application. For the most > part, sticking close to ANSI has made this pretty seamless. However, there > have been occasions where we want to write either DDL or DML in a >

Re: [sqlalchemy] Is there an event hook for putting a class into class_registry?

2021-10-10 Thread Mike Bayer
File "/codebase_mountpoint/codebase/database/__init__.py", line 177, in > set_serialize_map > for c in visible_table_columns: > File "/codebase_mountpoint/codebase/database/__init__.py", line 175, in > > lambda x: cls.get_property_name_

Re: [sqlalchemy] Is there an event hook for putting a class into class_registry?

2021-10-08 Thread Mike Bayer
stated previously, the best event to use when a class is first mapped but before the full span of mappings have been considered is the before_mapper_configured() event (can confirm _columntoproperty is available at that stage). > On Friday, October 8, 2021 at 6:58:02 AM UTC-7 Mike Bayer wro

Re: [sqlalchemy] Is there an event hook for putting a class into class_registry?

2021-10-08 Thread Mike Bayer
ablished. but also, the "__mapper__" attribute is a convenience feature that should not be relied upon at this stage. the correct way to get the mapper for a class is to use the sqlalchemy.inspect() function, i.e. mapper = inspect(class). > On Wednesday, October 6, 2021 at 5:36:22 AM U

Re: [sqlalchemy] Is there an event hook for putting a class into class_registry?

2021-10-06 Thread Mike Bayer
events that occur around this time include this one claims it's before: https://docs.sqlalchemy.org/en/14/orm/events.html#sqlalchemy.orm.MapperEvents.instrument_class this one says after: https://docs.sqlalchemy.org/en/14/orm/events.html#sqlalchemy.orm.InstrumentationEvents.class_instrument

Re: [sqlalchemy] 'validates' include_removes=True doesn't seem to prevent deletion?

2021-10-05 Thread Mike Bayer
On Mon, Oct 4, 2021, at 9:49 PM, niuji...@gmail.com wrote: > I have the following validator: > > @sa_orm.validates('variant_names', include_removes=True) > def validate_unique_identifying(self, key, name_dict, > is_remove): > if is_remove: >

Re: [sqlalchemy] Can `sqlalchemy.orm.validates` be set on multiple attributes?

2021-10-01 Thread Mike Bayer
_property.setter allowed to create new records like this way at all? > On Saturday, September 4, 2021 at 7:43:24 AM UTC-7 Mike Bayer wrote: >> >> >> On Fri, Sep 3, 2021, at 4:07 PM, niuji...@gmail.com wrote: >>> In the official documentation it

Re: [sqlalchemy] recursive cte use current row as anchor

2021-09-26 Thread Mike Bayer
)) AS anon_1 FROM staff so the UNION joins out to "lineage_nodes" again. I've not really worked with recursive CTEs much but i think that's the idea. On Sun, Sep 26, 2021, at 4:01 PM, Mike Bayer wrote: > i applied an alias to the CTE before unioning it, please take that out and >

Re: [sqlalchemy] recursive cte use current row as anchor

2021-09-26 Thread Mike Bayer
lineage_nodes.anchor_id = staff.id > ) > ) AS anon_1 > FROM > staff > > > On Sunday, September 26, 2021 at 7:35:20 AM UTC-7 Mike Bayer wrote: >> __ >> OK there's various small issues here but overall SQLAlchemy's new "nesting" >&g

Re: [sqlalchemy] recursive cte use current row as anchor

2021-09-26 Thread Mike Bayer
d).filter(recursive_cte.c._enabling_factor == None)) > > top_id = select(sasql.func.min(lineage_nodes.c.id)) > Staff.effective_enabling_factor = > column_property(select(final_s.c._enabling_factor).where( > final_s.c.record_id == top_id).scalar_subquery()) # This is where I &

Re: [sqlalchemy] recursive cte use current row as anchor

2021-09-25 Thread Mike Bayer
..) # I have trouble in > this line here. >## codes above has a NameError: name '__class__' is not defined > >team_members = relationship('Staff', >backref=backref( >'supervisor', >remote_sid

Re: [sqlalchemy] recursive cte use current row as anchor

2021-09-24 Thread Mike Bayer
this is a hefty query to dig in to but column_property() subqueries have to be formed in terms of a correlated subquery. So instead of injecting a particular primary key into it, you set it to point to the Staff.id column. correlated subqueries are not terrific performers and the construct can

Re: [sqlalchemy] UPSERT method for MS SQL Server

2021-09-22 Thread Mike Bayer
it is something that would ideally be available for the MS SQL and Oracle dialects. We've gotten by implementing specific "upsert" constructs for SQLite, Postgresql and MySQL that are not "MERGE". The thing is MERGE is SQL standard, although I'm not sure it's worth it to make "MERGE" a

Re: [sqlalchemy] Decoupling business and ORM model using dataclass(es)

2021-09-21 Thread Mike Bayer
, trying to sneak by with temporary instrumentation is not the way to do that. > > Kindest regards > > On Tuesday, September 21, 2021 at 3:51:36 PM UTC+2 Mike Bayer wrote: >> >> >> On Mon, Sep 20, 2021, at 11:40 PM, Nikola Radovanovic wrote: >>> Thank you

Re: [sqlalchemy] Decoupling business and ORM model using dataclass(es)

2021-09-21 Thread Mike Bayer
akkeydictionary, so if that's what you mean, then yes. again i am not famliiar with that book and im not really sure how code is not "maintainable" if it has some private attributes on it. seems pretty off. > > Kindest regards > > On Monday, September 20, 2021 at 4:04:05

Re: [sqlalchemy] Decoupling business and ORM model using dataclass(es)

2021-09-20 Thread Mike Bayer
On Mon, Sep 20, 2021, at 5:04 AM, Nikola Radovanovic wrote: > > Hi, > When decoupling business from ORM model, dataclass passed to SA's imperative > mapper *map_imperatively* will receive SA's internals like for example > *_sa_instance_state* and *_sa_class_manager*. > > I am wondering, what

Re: [sqlalchemy] Re: AsyncEngine always returns UTC time for datetime column

2021-09-15 Thread Mike Bayer
using the "sync" engine I would assume you are using psycopg2, which is going to use native Postgresql client libraries. asyncpg I think is written in cython and doesn't actually use libpq if im reading it correctly. therefore the difference may be a libpq related setting of the local timezone

Re: [sqlalchemy] Selectinload and bindparam

2021-09-10 Thread Mike Bayer
qlalchemy, it's really helpful and thanks to nested > options it's really fun to use. > wtorek, 31 sierpnia 2021 o 16:41:07 UTC+2 Mike Bayer napisał(a): >> __ >> >> this is true, the parameters can't be known to be passed along to a >> secondary query. the appr

Re: [sqlalchemy] Change in before_flush (maybe from 1.3 to 1.4?)

2021-09-09 Thread Mike Bayer
On Sun, Sep 5, 2021, at 6:41 PM, and...@acooke.org wrote: > > I'm having a hard time understanding some of my own code, which no longer > works. The only way I can see it having worked in the past is if > auto-flushing did not call before_flush, but commit did? Is that possible?

Re: [sqlalchemy] Can `sqlalchemy.orm.validates` be set on multiple attributes?

2021-09-04 Thread Mike Bayer
On Fri, Sep 3, 2021, at 4:07 PM, niuji...@gmail.com wrote: > In the official documentation it says: > > > *validates* > (*names, > **kw) > > > Decorate a method as a ‘validator’ for one or more named

Re: [sqlalchemy] Issue with mypy and the new version of alembic (1.7.1 agaist 1.6.5)

2021-09-02 Thread Mike Bayer
this is issue https://github.com/sqlalchemy/alembic/issues/897 and we don't know how to fix it. suggestions welcome. On Thu, Sep 2, 2021, at 3:32 AM, Stéphane Brunner wrote: > Hello, > > In the new version, I get the following error with my mypy check: > > > > Traceback (most recent call

Re: [sqlalchemy] ORM query returning separate columns instead of objects

2021-09-01 Thread Mike Bayer
you can take the statement and execute it on the connection instead, then you'll get tuples with columns. not sure if there was some way to get this through session.execute() directly. On Tue, Aug 31, 2021, at 5:01 PM, Gord Thompson wrote: > My memory is failing me. I seem to recall that

Re: [sqlalchemy] Why was the column information stripped away in SA 1.4.20 that existed in 1.3.20?

2021-09-01 Thread Mike Bayer
User.name) >>>> df = pd.DataFrame(result_set) >>>> print(df) >>>> """ >>>> id name >>>> 0 1 Gord >>>> 1 2 Bob >>>> """ >>>> *With version 1.4:* >>>> >>>&

Re: [sqlalchemy] SqlAlchemy 1.4 and Sequences

2021-09-01 Thread Mike Bayer
c_info[1], with_traceback=exc_info[2]) > File "C:\Python36\lib\site-packages\sqlalchemy\util\compat.py", line 207, > in raise_ > raise exception > File "C:\Python36\lib\site-packages\sqlalchemy\engine\base.py", line 1752, > in _execute_context > cu

Re: [sqlalchemy] Why was the column information stripped away in SA 1.4.20 that existed in 1.3.20?

2021-08-31 Thread Mike Bayer
On Tue, Aug 31, 2021, at 3:10 PM, Terrence-Monroe: Brannon wrote: > > Creating a pandas dataframe that contained descriptive column names formerly > was as easy as: > > result_set = session.query(cls.column_1) > df = pandas.Dataframe(result_set) > print df.column_1 > > but while this works

Re: [sqlalchemy] Selectinload and bindparam

2021-08-31 Thread Mike Bayer
)) >> or >> s = select(X).options(joinedload(A).options(joinedload(B))) >> result = session.execute(s, dict(c_global_attr_1=1)) >> >> s = s.options(*selectionload*(A.b_filtered_rel)) >> or >> s = select(X).options(*selectionload*(A).options(joinedload(B)

Re: [sqlalchemy] SqlAlchemy 1.4 and Sequences

2021-08-31 Thread Mike Bayer
issue https://github.com/sqlalchemy/sqlalchemy/issues/6963 is added to deal with possibly multiple failure modes in this case. On Tue, Aug 31, 2021, at 10:24 AM, Mike Bayer wrote: > Hi - > > Your test case is omitting a critical detail which we would assume is you are

Re: [sqlalchemy] SqlAlchemy 1.4 and Sequences

2021-08-31 Thread Mike Bayer
Hi - Your test case is omitting a critical detail which we would assume is you are using the "implicit_returning=False" flag on your create_engine().dont use this flag as it serves no useful purpose and that will fix your issue here, we will be deprecating and removing this flag. will try

Re: [sqlalchemy] Selectinload and bindparam

2021-08-30 Thread Mike Bayer
yes, you would use bindparam("some_name") in conjunction with the "and_()" feature, like options(selectinload(MyClass.foo.and_(MyClass.name == bindparam("some_name" On Mon, Aug 30, 2021, at 10:55 AM, Tomas Pavlovsky wrote: > Hello, > > is it possible in sqlalchemy arguments from

Re: alembic upgrade heads

2021-08-25 Thread Mike Bayer
seems like you should ensure that Alembic is installed in the current Python environment. looks like it is not installed correctly. On Wed, Aug 25, 2021, at 2:23 PM, Vinod Rao wrote: > Hi all: > > I have been struggling to solve the following error: > > File "/virtual_env/bin/alembic",

Re: [sqlalchemy] Combination of (pg) on_conflict_do_update and ORM's bulk_insert_mappings

2021-08-24 Thread Mike Bayer
not exactly what you asked for but this seems pretty easy to use: https://docs.sqlalchemy.org/en/14/orm/persistence_techniques.html#using-postgresql-on-conflict-with-returning-to-return-upserted-orm-objects On Tue, Aug 24, 2021, at 2:53 PM, Mike Bayer wrote: > let me try to make an exam

Re: [sqlalchemy] Combination of (pg) on_conflict_do_update and ORM's bulk_insert_mappings

2021-08-24 Thread Mike Bayer
let me try to make an example On Tue, Aug 24, 2021, at 1:36 PM, Mike Bayer wrote: > you can use a mapped class directly with pg's insert() construct: > > insert(MyClass).values(...).on_conflict(...) > > are you referring to being able to pass instances of mapped objects to the &

Re: [sqlalchemy] Combination of (pg) on_conflict_do_update and ORM's bulk_insert_mappings

2021-08-24 Thread Mike Bayer
you can use a mapped class directly with pg's insert() construct: insert(MyClass).values(...).on_conflict(...) are you referring to being able to pass instances of mapped objects to the values? Just pull these out of each object's `__dict__`, it's much more efficient than

Re: [sqlalchemy] Configuring Alembic in SQL-only mode

2021-08-22 Thread Mike Bayer
it should be fine though I'm surprised PostgreSQL isn't forcing you to ROLLBACK the transaction when one of those operations fails.if it isn't then should be fine for now. I'd probably make a function that does the command execution that you import from somewhere else though, that way your

Re: [sqlalchemy] prevent (raise exceptions) on bytestring values for non-byte types

2021-07-29 Thread Mike Bayer
The Unicode datatype will emit a warning if you pass it a bytestring. you can use that instead of String, or use a datatype with your own assertions based on https://docs.sqlalchemy.org/en/14/core/custom_types.html#coercing-encoded-strings-to-unicode On Thu, Jul 29, 2021, at 5:17 PM,

Re: [sqlalchemy] Relationship backref cascade path

2021-07-24 Thread Mike Bayer
On Sat, Jul 24, 2021, at 5:49 AM, Lele Gaifax wrote: > Hi all, > > I need some clarification on the following RemovedIn20Warning > > "Pet" object is being merged into a Session along the backref cascade path > for relationship "Person.pets"; in SQLAlchemy 2.0, this reverse cascade will >

Re: [sqlalchemy] async mode and relationship more than one level deep

2021-07-21 Thread Mike Bayer
you need to chain those like: selectinload(Database.person).selectinload(Person.city) docs: https://docs.sqlalchemy.org/en/14/orm/loading_relationships.html#relationship-loading-with-loader-options On Wed, Jul 21, 2021, at 4:25 PM, sector119 wrote: > Hello, Mike > > When I want to use some

Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-20 Thread Mike Bayer
that distinguish between the "dupe" rows and use the association object pattern for persistence. > > вторник, 20 июля 2021 г. в 16:10:12 UTC+3, Mike Bayer: >> __ >> if you are mapping ORM classes to the same table that is also used as the >> "secondary" table

Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-20 Thread Mike Bayer
) children = relationship("Child", secondary=association_table) class Child(Base): __tablename__ = 'right' id = Column(Integer, primary_key=True) > > P.S. It seems the error to be raised implicit, because "ONLY 2 were matched" > понедельник, 19 июля 202

Re: [sqlalchemy] sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

2021-07-19 Thread Mike Bayer
This is all expected behavior, the main reason you're having problems is that you are using multiple sessions and mixing their results together.If you need to do this, there are few approaches, the most basic being to use the merge() method:

<    1   2   3   4   5   6   7   8   9   10   >