Foros del Web » Programando para Internet » Python »

Error "Commands out of sync" en Python con SQLAlchemy

Estas en el tema de Error "Commands out of sync" en Python con SQLAlchemy en el foro de Python en Foros del Web. CONTEXTO. Python 3.9.1, Flask-MySQLdb 0.2.0, Flask-SQLAlchemy 2.5.1. No utilizo procedimientos almacenados. PROBLEMA. Tengo un datatables que lista todos mis usuarios, y cada fila contiene un ...
  #1 (permalink)  
Antiguo 15/11/2021, 12:38
Avatar de berkeleyPunk  
Fecha de Ingreso: febrero-2013
Ubicación: México :C
Mensajes: 565
Antigüedad: 11 años, 2 meses
Puntos: 22
Pregunta Error "Commands out of sync" en Python con SQLAlchemy

CONTEXTO. Python 3.9.1, Flask-MySQLdb 0.2.0, Flask-SQLAlchemy 2.5.1. No utilizo procedimientos almacenados.

PROBLEMA. Tengo un datatables que lista todos mis usuarios, y cada fila contiene un link para eliminar el registro. Elimino un registro sin problema (uso Ajax). Pero si intento eliminar un segundo registro, me saltan alternativamente alguno de estos errores SQLAlchemy:
  1. 2014, "Commands out of sync
  2. This session is in 'prepared' state; no further SQL can be emitted within this transaction
  3. Can't reconnect until invalid transaction is rolled back


QUÉ ES LO QUE ESTOY HACIENDO (muy abreviado).

Controlador

Código Python:
Ver original
  1. @blueprint.route('/delete/<id>', methods=['POST'])
  2. def delete(id):
  3.     if (request.method == 'POST'):
  4.         User.delete(id)
  5.         data = {
  6.             'estatus': True,
  7.             'msg': 'Usuario eliminado'
  8.         }
  9.         return jsonify(data)

Modelo

Código Python:
Ver original
  1. engine = create_engine(SQLALCHEMY_DATABASE_URI)
  2. Session = sessionmaker(engine)
  3. sessionDB = Session()
  4. def delete(id):
  5.     sessionDB.query(User).filter(User.id == id).delete()
  6.     sessionDB.commit()

CÓMO HE INTENTADO SOLUCIONAR EL PROBLEMA (sin éxito).
He intentado lo siguiente, según las recomendaciones que se hacen en otros lugares en internet:
  1. La línea del engine la he cambiado por:
    Código Python:
    Ver original
    1. engine = create_engine(SQLALCHEMY_DATABASE_URI, poolclass=NullPool)
  2. La línea del engine la he cambiado por:
    Código Python:
    Ver original
    1. engine = create_engine(SQLALCHEMY_DATABASE_URI, pool=QueuePool(reset_on_return=False))
  3. Después del sessionDB.commit() he puesto las siguientes líneas (probando ponerlas por separado, a veces todas, a veces sólo dos, etc):

1. sessionDB.close()
2. engine.dispose()
3. sessionDB.flush()


Estoy atorado con esto. Cualquier sugerencia es totalmente bienvenida.

Saludos!

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 13:28.