Re: [sqlalchemy] Re: Conditional insert in one transaction

2021-02-03 Thread Mike Bayer
for inserts, not really. if you want to guard against duplicate, the pattern is use a savepoint (begin_nested()), catch integrity error, then use the new row if someone else inserted it.this is the more performant option as well as opposed to pessimistic locking.

Re: [sqlalchemy] Re: Conditional insert in one transaction

2021-02-03 Thread Vitaly Kruglikov
How can the query/merge or query/add be performed atomically? What would happen if between the two calls another process inserted a row with the same unique key? Wouldn't the `s.merge()` then trigger a `psycopg2.errors.UniqueViolation` exception (in postgres case) when the new row insert is

Re: [sqlalchemy] Invalidated Collection

2021-02-03 Thread Mike Bayer
On Wed, Feb 3, 2021, at 6:23 PM, Christian Henning wrote: > Hi Mike, > > thanks for your advice! I'll make the changes. > > But let me ask you one thing. My classmethod create() is but more complex > than I have posted. It's meant to catch IntegrityError so that unique > constraints are

Re: [sqlalchemy] Invalidated Collection

2021-02-03 Thread Christian Henning
Hi Mike, thanks for your advice! I'll make the changes. But let me ask you one thing. My classmethod create() is but more complex than I have posted. It's meant to catch IntegrityError so that unique constraints are enforced. Image a User table has a "unique" constraint on the name. When I

Re: [sqlalchemy] Invalidated Collection

2021-02-03 Thread Mike Bayer
the session.commit() method expires all attributes by default: https://docs.sqlalchemy.org/en/13/orm/session_basics.html#committing your code is organized in an unusual way such that transactions are being committed inside of attribute assignment operations:

[sqlalchemy] Invalidated Collection

2021-02-03 Thread Christian Henning
I don't understand why SQLAlchemy gives me the following warning: SAWarning: This collection has been invalidated. util.warn("This collection has been invalidated.") Here is a minimal code example: - - import sqlalchemy print(sqlalchemy.__version__) from

Re: [sqlalchemy] Creating column SQLAlchemy property on parent class based on child column property

2021-02-03 Thread Mike Bayer
I think what the OP would have to do is write a UNION, that is: s.query(Child1).filter(Child1.thing == "thing").union( s.query(Child2).filter(Child2.thing == "thing") ) that's how this problem is approached in SQL.With SQLAlchemy, any problem should be solved by considering the desired

Re: [sqlalchemy] Creating column SQLAlchemy property on parent class based on child column property

2021-02-03 Thread Simon King
I don't think you're going to find a way to do that built in to SQLAlchemy. When you write "session.query(Parent)", SQLAlchemy constructs a query against the "parent" table. But to filter by your "is_done" property, it would suddenly need to join every child table into the query and construct a

[sqlalchemy] Creating column SQLAlchemy property on parent class based on child column property

2021-02-03 Thread Mehrdad Pedramfar
The structure I have created is like below:     class Parent(Base):        __tablename__ = 'parent'            id = Field(            Integer,            primary_key=True        )            type_ = Field(            String(50),            readonly=True        )            __mapper_args__ = {