Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Last active June 28, 2016 20:11
Show Gist options
  • Select an option

  • Save inklesspen/4205802bfffcdabfa737 to your computer and use it in GitHub Desktop.

Select an option

Save inklesspen/4205802bfffcdabfa737 to your computer and use it in GitHub Desktop.

Revisions

  1. inklesspen revised this gist Jan 5, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion revised_models.py
    Original file line number Diff line number Diff line change
    @@ -43,4 +43,4 @@ def includeme(config):
    # Every request will have a db_session property. The first time
    # you use it, it creates a session, and then replaces the property
    # with that session.
    config.add_request_method(lambda request: sessionmaker(), 'db_session', reify=True)
    config.add_request_method(lambda request: maker(), 'db_session', reify=True)
  2. inklesspen renamed this gist Jan 4, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. inklesspen created this gist Jan 4, 2015.
    46 changes: 46 additions & 0 deletions revised_model.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    from sqlalchemy import (
    Column,
    Index,
    Integer,
    Text,
    )

    from sqlalchemy.ext.declarative import declarative_base

    from sqlalchemy import engine_from_config
    from sqlalchemy.orm import sessionmaker

    from zope.sqlalchemy import register

    Base = declarative_base()


    class MyModel(Base):
    __tablename__ = 'models'
    id = Column(Integer, primary_key=True)
    name = Column(Text)
    value = Column(Integer)

    Index('my_index', MyModel.name, unique=True, mysql_length=255)


    def includeme(config):
    # We'll configure the DB connection from this.
    settings = config.get_settings()

    # by including pyramid_tm here, it doesn't need to be in the ini file.
    config.include('pyramid_tm')

    # connect to the DB and make a sessionmaker
    engine = engine_from_config(settings, 'sqlalchemy.')
    maker = sessionmaker()
    # register the sessionmaker with the zope transaction manager
    # all sessions created from this will be hooked up
    register(maker)
    # and finally connect the session to the engine
    maker.configure(bind=engine)

    # Every request will have a db_session property. The first time
    # you use it, it creates a session, and then replaces the property
    # with that session.
    config.add_request_method(lambda request: sessionmaker(), 'db_session', reify=True)