SQLAlchemy provides a very clean interface for defining database tables:
engine = create_engine('sqlite:///:memory:') metadata = MetaData() user = Table('user', metadata, Column('user_id', Integer, primary_key = True), Column('user_name', String(16), nullable = False), Column('email_address', String(60), key='email'), Column('password', String(20), nullable = False) ) user_prefs = Table('user_prefs', metadata, Column('pref_id', Integer, primary_key=True), Column('user_id', Integer, ForeignKey("user.user_id"), nullable=False), Column('pref_name', String(40), nullable=False), Column('pref_value', String(100)) )
And once these tables are defined, it is very easy to create these tables using the metadata.create_all(engine) function. This is especially good for testing when you want to create tables that will not interfere with existing tables used in the production process.
The project I'm working on now is heavily dependent on custom functions in postgres. Is there an easy way to define these functions using SQLAlchemy so that metadata.create_all(engine) correctly creates the functions along with the corresponding tables?
smang source share