Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:49:05+00:00 2026-06-09T16:49:05+00:00

I am using the sqlalchemy expression language for its notation and connection pooling to

  • 0

I am using the sqlalchemy expression language for its notation and connection pooling to create dao objects for communicating with the persistence layer. I wanted to get some opinions on how I should approach setting up the metadata and engine so that they are available to the applications view callables. According to sqlalchemy’s documentation http://docs.sqlalchemy.org/en/rel_0_7/core/connections.html, they are typically bound and declared global, however I’ve neither this or the singleton approach are good ideas. Any thoughts would be appreciated…

This is what my __init__.py file looks like inside my project’s directory:

from pyramid.config import Configurator
from sqlalchemy import engine_from_config, MetaData, create_engine
from pyramid_beaker import session_factory_from_settings

db_url = 'postgresql://user:password@localhost/dbname'
engine = create_engine(db_url)
meta = MetaData()

def main(global_config, **settings):
    meta.bind = engine
    .
    .
    .
    [other configuration settings]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:49:07+00:00Added an answer on June 9, 2026 at 4:49 pm

    The Pyramid documentation includes a tutorial on integrating Pyramid with SQLAlchemy.

    There are two special packages that integrate SQLAlchemy transactions and session management with Pyramid, pyramid_tm and zope.sqlalchemy. These together take care of your sessions:

    from sqlalchemy import engine_from_config
    
    from .models import DBSession
    
    def main(global_config, **settings):
        """This function returns a Pyramid WSGI application."""
    
        engine = engine_from_config(settings, 'sqlalchemy.')
        DBSession.configure(bind=engine)
        # Configuration setup
    

    Here we take the configuration settings from your .ini configuration file; and in models.py:

    from sqlalchemy.ext.declarative import declarative_base
    
    from sqlalchemy.orm import (
        scoped_session,
        sessionmaker,
        )
    
    from zope.sqlalchemy import ZopeTransactionExtension
    
    DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
    Base = declarative_base()
    
    class YourModel(Base):
        # Define your model
    

    Note the use of a scoped_session there, using the transaction extension to integrate with Pyramid.

    Then in views, all you need to do is use the DBSession session factory to get your sessions:

    from pyramid.view import view_config
    from .models import (
        DBSession,
        YourModel,
        )
    
    @view_config(...)
    def aview(request):
        result = DBSession.query(YourModel).filter(...).first()
    

    Committing and rolling back will be integrated with the request; commit on 2xx and 3xx, rollback on exceptions, for example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using sqlalchemy with MySQL , and executing query with sql expression. When
I'm using SqlAlchemy to store some objects with a DateTime field: my_date = Field(DateTime())
I create a table in database using sqlalchemy and now want to make a
I am running an app that using sqlalchemy to access db layer. now I
I want to create a desktop application using SQLAlchemy and wxPython, but I'd like
I'm using sqlalchemy with postgresql. And I'm newbie of sqlalchemy. I made forien key
I'm using SQLalchemy for a Python project, and I want to have a tidy
Hi im using sqlalchemy on a db2 table with 500k rows. using plain sql
I am using sqlalchemy job store in APS . Jobs are added too cron
I'm working on a Python server application using SQLAlchemy for database access. I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.