Hi All,
How do I create a postgres sequence independent of a table using sqlalchemy?
How do I select the next value from a sequence that forms part of a
postgres table, starting with the SQLAlchemy Table object?
cheers,
Chris
--
Simplistix - Content Management, Batch Processing Python
ad 1)
from http://docs.sqlalchemy.org/en/rel_0_7/core/schema.html#defining-sequences :
The Sequence object also has the ability to be executed standalone
like a SQL expression, which has the effect of calling its “next
value” function:
seq = Sequence('some_sequence')
nextid =
working example for the first question:
from sqlalchemy import Sequence, create_engine, MetaData
if __name__ == __main__:
md = MetaData()
e = create_engine('postgresql://rforkel@/test1', echo=True)
md.bind = e
s = Sequence('name', metadata=md)
md.create_all()
print
example now includes your second question:
from sqlalchemy import Sequence, create_engine, MetaData, Column, Integer
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class Model(Base):
__tablename__ = 'model'
col = Column(Integer, Sequence('seq'),
Thanks Michael,
Just to make clear what exactly begin_nested() is contributing:
Normal case:
session.rollback() goes back to the last session.commit()
session.begin_nested() case:
session.rollback() goes back to the last session.begin_nested() or
session.commit(), whichever occurred last.
engine.dispose() is an inefficient operation, as the Engine is a
factory for connections, not a connection itself. If you'd like
there to be zero actual database connections open when the application
is idle, you can disable pooling using NullPool.
Very good point. Will do!
To be more