Re: [sqlalchemy] Not able to filter column by datetime
Thank you Michael, I think sometimes I am blind xD Regards El miércoles, 15 de abril de 2015, 15:13:38 (UTC+2), Michael Bayer escribió: On 4/15/15 6:52 AM, Juan Antonio Ibáñez wrote: Hello, I have dozens of queries filtering DateTime columns but I have got one I don't know why it doesn't work. I have: --- q = DBSession.query(func.sum(RecargaCredito.importe), Local.nombre)\ .join((Local, RecargaCredito.locales_id == Local.id)) fl_desde = (datetime.now() - timedelta(days=7)).replace(hour=0, minute=0, second=0) fl_hasta = datetime.now().replace(hour=23, minute=59, second=59) q = q.filter(RecargaCredito.fechayhora = fl_desde).\ filter(RecargaCredito.fechayhora = fl_hasta) q = q.group_by(RecargaCredito.locales_id) f = q.all() --- RecargaCredito looks: --- class RecargaCredito(DeclarativeBase): __tablename__ = 'recargas_credito' __table_args__ = {'mysql_engine':'InnoDB'} id=Column(Integer, primary_key=True) fechayhora = DateTime importe = Column(DECIMAL(9,2)) locales_id=Column(Integer, ForeignKey('locales.id')) local = relation('Local', remote_side = 'Local.id') --- and I get error 'TypeError: can't compare datetime.datetime to VisitableType' you seem to be missing Column() for fechayhora: fechayhora = Column(DateTime) I've checked fl_desde and fl_hasta are python datetime objects Do you know what that error says? Regards -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.com javascript:. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Not able to filter column by datetime
Hello, I have dozens of queries filtering DateTime columns but I have got one I don't know why it doesn't work. I have: --- q = DBSession.query(func.sum(RecargaCredito.importe), Local.nombre)\ .join((Local, RecargaCredito.locales_id == Local.id)) fl_desde = (datetime.now() - timedelta(days=7)).replace(hour=0, minute=0, second=0) fl_hasta = datetime.now().replace(hour=23, minute=59, second=59) q = q.filter(RecargaCredito.fechayhora = fl_desde).\ filter(RecargaCredito.fechayhora = fl_hasta) q = q.group_by(RecargaCredito.locales_id) f = q.all() --- RecargaCredito looks: --- class RecargaCredito(DeclarativeBase): __tablename__ = 'recargas_credito' __table_args__ = {'mysql_engine':'InnoDB'} id=Column(Integer, primary_key=True) fechayhora = DateTime importe = Column(DECIMAL(9,2)) locales_id=Column(Integer, ForeignKey('locales.id')) local = relation('Local', remote_side = 'Local.id') --- and I get error 'TypeError: can't compare datetime.datetime to VisitableType' I've checked fl_desde and fl_hasta are python datetime objects Do you know what that error says? Regards -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] DateTime format
Hello, I am using Sqlalchemy under Turbogears and I'd like to know which is the best way to customize column to string conversion of a DateTime column. I need to do it in an automatic way instead having to format it before showing. Regards -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/cU712dWA_kMJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Count over aliased JOIN
Hello I am joining one table with itself so I am using alias as: alias_usuarios1 = aliased(Usuario) alias_usuarios2 = aliased(Usuario) usuarios = DBSession.query(alias_usuarios1, alias_usuarios2.nombre).filter(alias_usuarios1.resellers_id == alias_usuarios2.id) All works ok except when I try to count how many rows I'd got. If I apply count() to previous query I get following SQL code: SELECT count(1) AS count_1 FROM (SELECT usuarios.id AS usuarios_id, usuarios_1.nombre AS usuarios_1_nombre FROM usuarios, usuarios AS usuarios_1, usuarios AS usuarios_2 WHERE usuarios_2.resellers_id = usuarios_1.id) AS anon_1 It is joining three tables instead two to make de JOIN. Is there any way to mend it? Regards -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Subtransactions problem
Hello! I am dealing with Turbogears and SQL Alchemy. I need to make one import from a CSV file to a MySQL DB. I need also to controll which rows fails on import. I am working with the next code inside a loop: try: session.begin(subtransactions=True) session.add(importe) session.flush() nimportados += 1 except Exception as ex: log.debug(ex) session.rollback() nerroneos += 1 During exec I get: - 12:55:45,598 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] BEGIN 12:55:45,600 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] INSERT INTO importes (tarifas_id, prefijo, destino, importe, tipo, pc, psi, ps, sg) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s) 12:55:45,600 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] [u'1', 'PREFIJO', 'DESTINO', 'IMPORTE', u'', 0, 60, 60, 0] 12:55:45,601 INFO [sqlalchemy.engine.base.Engine.0x...f1ac] ROLLBACK 12:55:45,603 DEBUG [svbpanel.controllers] (IntegrityError) (1062, Duplicate entry 'PREFIJO' for key 'prefijo') u'INSERT INTO importes (tarifas_id, prefijo, destino, importe, tipo, pc, psi, ps, sg) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)' [u'1', 'PREFIJO', 'DESTINO', 'IMPORTE', u'', 0, 60, 60, 0] 12:55:45,603 DEBUG [svbpanel.controllers] The transaction is inactive due to a rollback in a subtransaction. Issue rollback() to cancel the transaction. - Peopel on TG2 group told me about the appearance of not init the subtransaction. I thing TG2 init a own transaction in an automatic way so I should work with subtransactions (I think) Commiting instead flushing doesn't work... Any idea about this problem? Regards -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalch...@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.