declarative

SQLAlchemy update if unique key exists

SQLAlchemy update if unique key exists Question: I’ve got a class: class Tag(Base, TimestampMixin): """Tags""" __tablename__ = ‘tags’ __table_args__ = {‘mysql_engine’ : ‘InnoDB’, ‘mysql_charset’ : ‘utf8’ } id = Column(Integer(11), autoincrement = True, primary_key = True) tag = Column(String(32), nullable = False, unique = True) cnt = Column(Integer(11), index = True, nullable = False, default …

Total answers: 3

SQLAlchemy: a better way for update with declarative?

SQLAlchemy: a better way for update with declarative? Question: Let’s say I have a user table in declarative mode: class User(Base): __tablename__ = ‘user’ id = Column(u’id’, Integer(), primary_key=True) name = Column(u’name’, String(50)) When I know user’s id without object loaded into session, I update such user like this: ex = update(User.__table__).where(User.id==123).values(name=u"Bob Marley") Session.execute(ex) I …

Total answers: 3