[sqlalchemy] Retrive datetime attributes
Hi all, I'm using Python 2.6.5 and SQLAlchemy-0.7.8 over sqlite3 to store and retrieve logs with in table like this : class LogEntry(Base): Log class __tablename__ = 'log' #common data id = Column(Integer, primary_key=True) timestamp = Column(DateTime()) When querying back object, how comes I get unicode string in timestamp attribute ? Isn't SA supposed to convert ISO formatted string stored in sqlite back to python datetime object ? -- You received this message because you are subscribed to the Google Groups sqlalchemy group. 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.
Re: [sqlalchemy] Re: Retrive datetime attributes
Thanks! Here is a slightly modified version that shows what happens : if querying in another session, with a from_statement, the string is not processed. Is it the expected behaviour ? from datetime import datetime from sqlalchemy import Column, DateTime, Integer, create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base engine = create_engine('sqlite://', echo=True) Base = declarative_base(engine) Session = sessionmaker() class LogEntry(Base): Log class __tablename__ = 'log' #common data id = Column(Integer, primary_key=True) timestamp = Column(DateTime) def __init__(self,timestamp): self.timestamp = timestamp log = LogEntry(timestamp=datetime.now()) Base.metadata.create_all() session = Session() session.add(log) session.commit() session.close() session = Session() log_1 = session.query(LogEntry).one() session.close() session = Session() log_2 = session.query(LogEntry).from_statement(select * from log).one() session.close() print type(log_1.timestamp) print type(log_2.timestamp) On 21 June 2012 15:35, GHZ geraint.willi...@gmail.com wrote: Here is code that works for me: from datetime import datetime from sqlalchemy import Column, DateTime, Integer, create_engine from sqlalchemy.orm import sessionmaker from sqlalchemy.ext.declarative import declarative_base engine = create_engine('sqlite://', echo=True) Base = declarative_base(engine) Session = sessionmaker() session = Session() class LogEntry(Base): Log class __tablename__ = 'log' #common data id = Column(Integer, primary_key=True) timestamp = Column(DateTime) def __init__(self): self.timestamp = datetime.now() log = LogEntry() Base.metadata.create_all() session.add(log) session.flush() log = session.query(LogEntry).one() print type(log.timestamp) On Thursday, June 21, 2012 2:35:24 PM UTC+2, Fabien Ribes wrote: Hi all, I'm using Python 2.6.5 and SQLAlchemy-0.7.8 over sqlite3 to store and retrieve logs with in table like this : class LogEntry(Base): Log class __tablename__ = 'log' #common data id = Column(Integer, primary_key=True) timestamp = Column(DateTime()) When querying back object, how comes I get unicode string in timestamp attribute ? Isn't SA supposed to convert ISO formatted string stored in sqlite back to python datetime object ? -- 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/-/N6IlGbBTzyUJ. 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. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. 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] Deletion
Hi all, Despite some doc and web digging, I didn't find how to tell sqa to behave the way I want : on deletion on Peripheral, also delete in Actuator. with the following code, the record in Actuator remains after a deletion, and a subsequent creation fails with IntegrityError. class Peripheral(Base): __tablename__ = 'peripheral' id = Column(Integer, primary_key=True) label = Column(String(20), nullable=False) __mapper_args__ = {'polymorphic_on': peripheral_type, 'polymorphic_identity': 'peripheral'} class Actuator(Peripheral): __tablename__ = 'actuator' __mapper_args__ = {'polymorphic_identity': 'actuator'} id = Column(None, ForeignKey('peripheral.id'), primary_key=True) duration = Column(Integer) Any suggestion ? Regards, -- You received this message because you are subscribed to the Google Groups sqlalchemy group. 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.
Re: [sqlalchemy] Re: Speed matters
Hi Sergey, I'll give it a try, thanks ! On 2 June 2011 03:15, Sergey V. sergey.volob...@gmail.com wrote: Hi, One easy/obvious improvement would be to delete all user's identifiers and groups at once without iterating over them for every user. Actually, iterating over the list of user_ids is not necessary too I think. code session.query(Identifier).filter(Identifier.user_id.in_(user_ids)).delete() session.query(User).filter(User.id.in_(user_ids)).delete() /code I'm not sure about deleting groups in your code - I suppose you don't want to delete the actual group but only the association between the user and the group, i.e. the record from the intermediate table. But the idea is the same - .filter(blah- blah.user_id.in_(user_ids)).delete() An even better solution would be to set up proper cascade rules on your relationships so all dependent items are deleted automatically when a user is deleted. Then the method will be a one-liner: code session.query(User).filter(User.id.in_(user_ids)).delete() /code -- You received this message because you are subscribed to the Google Groups sqlalchemy group. 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.