[sqlalchemy] help writing a query with conditions based on two tables/models using ORM

2020-06-26 Thread Corey Boyle
I have something like the following... class TimestampMixin(object): created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow) modified_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow) class Memo(db.Model, TimestampMixin):

[sqlalchemy] Re: Am I doing this query of lots of columns correctly?

2020-06-26 Thread 'Jonathan Vanasco' via sqlalchemy
that should be `loaded_columns_as_dict()` , unless you decorate the method with @property. -- 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

[sqlalchemy] Re: Am I doing this query of lots of columns correctly?

2020-06-26 Thread 'Jonathan Vanasco' via sqlalchemy
I use a mixin class to handle this stuff. Example below. > So, my question: is it generally better practice to name every column that you want to pull, even if it's a long list? Not really. There is a "bundle" api here that might be better for you-

Re: [sqlalchemy] MSSQL with PyODBC: StaleDataError on UPDATE statement

2020-06-26 Thread Mike Bayer
triggers, ah. Maybe? that seems inconvenient. it would be good if you could adjust the triggers to not affect the rowcount of the original statement, otherwise you'd have to set supports_sane_rowcount=False on your engine: engine.dialect.supports_sane_rowcount =False On Fri, Jun 26, 2020, at

Re: [sqlalchemy] MSSQL with PyODBC: StaleDataError on UPDATE statement

2020-06-26 Thread jue...@gmail.com
I think ID is the only primary key of the Measurements table, but I can double check this on Monday. I used sqlacodegen to create the initial models, so if this tool correctly detects primary keys, the Measurement model should match the table definition. I also tried to execute the follwing

Re: [sqlalchemy] Re: MSSQL with PyODBC: StaleDataError on UPDATE statement

2020-06-26 Thread Mike Bayer
yeah that is just disabling the check, the "5000 rows matched" is a critical malfunction referring to non-working primary key. On Fri, Jun 26, 2020, at 10:39 AM, jue...@gmail.com wrote: > Strangely, if I add the following mapper_args, it is working with a warning > (SAWarning: Dialect

Re: [sqlalchemy] MSSQL with PyODBC: StaleDataError on UPDATE statement

2020-06-26 Thread Mike Bayer
hi - does your Measurement table have a primary key constraint present, and does this primary key constraint consist of exactly the "ID" column only and no other columns? it would appear you have not mapped the primary key correctly. On Fri, Jun 26, 2020, at 9:57 AM, jue...@gmail.com wrote:

[sqlalchemy] Re: MSSQL with PyODBC: StaleDataError on UPDATE statement

2020-06-26 Thread jue...@gmail.com
Strangely, if I add the following mapper_args, it is working with a warning (SAWarning: Dialect mssql+pyodbc does not support updated rowcount - versioning cannot be verified.) but not with an exception. __mapper_args__ = { 'version_id_col': Created, 'version_id_generator':

[sqlalchemy] Re: MSSQL with PyODBC: StaleDataError on UPDATE statement

2020-06-26 Thread jue...@gmail.com
Here is the code I used (simplified): class Measurement(Base): __tablename__ = 'Measurement' __table_args__ = ( {'implicit_returning': False}, # Required because of some triggers on this table ) ID = Column(Integer, primary_key=True) Created = Column(DATETIME2,

[sqlalchemy] MSSQL with PyODBC: StaleDataError on UPDATE statement

2020-06-26 Thread jue...@gmail.com
I'm currently working with sqlalchemy (Version 1.3.18) to access a Microsoft Server SQL Database (Version: 14.00.3223). I use pyodbc (Version 4.0.30) When I try to update a single object and try to update and commit the object, I run into a StaleDataError: StaleDataError: UPDATE statement on