'No SQL UPDATE command while committing, after changes on an object

I have a table on the Microsoft SQL Server 2014 Express and I want to interact with this table using a flask application.

I have a problem that after a COMMIT command a UPDATE is non-deterministically invoked, sometimes occurs but more often not.

Below I am attaching a minimal example, where the problem occurs

#!/usr/bin/env python3
import sys
import time

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

from config import Config
# PYODBC_URI = "DRIVER={ODBC Driver 17 for SQL Server};(...others parameters...)"
# params = urllib.parse.quote_plus(PYODBC_URI)
# SQLALCHEMY_EXTERNAL_DATABASE_URI = "mssql+pyodbc:///?odbc_connect=%s" % params
# SQLALCHEMY_BINDS = {
#    'db': SQLALCHEMY_EXTERNAL_DATABASE_URI
# }

app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)

class SQLRamki(db.Model):
    __tablename__ = 'sqlramki'
    __bind_key__ = 'db'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    id_ramki_zlecenia = db.Column(db.Integer)

iters = 3
while iters:
    iters = iters - 1

    a = SQLRamki.query.filter_by(id=96).first()
    print(a.brief())

    a.id_ramki_zlecenia = 100000 - iters
    print(db.session.dirty)

    time.sleep(2)
    db.session.commit()

After the first call, I am getting

<SQLRamki id: 96 id_ramki_zlecenia: 89600>
IdentitySet([])
<SQLRamki id: 96 id_ramki_zlecenia: 99998>
IdentitySet([])
<SQLRamki id: 96 id_ramki_zlecenia: 99999>
IdentitySet([])

and if I invoke this script again, I am getting the same output

<SQLRamki id: 96 id_ramki_zlecenia: 89600>
IdentitySet([])
<SQLRamki id: 96 id_ramki_zlecenia: 99998>
IdentitySet([])
<SQLRamki id: 96 id_ramki_zlecenia: 99999>
IdentitySet([])

so, there wasn't invoked UPDATE on the DB (I had checked by setting a SQLALCHEMY_ECHO to True)

What am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source