0

I'm struggling to figure out why the session I'm getting after pyramid's bootstrap is refusing to execute queries, raising the transaction.interfaces.NoTransaction exception.

I'm trying to create a script using the pyramid configuration, but working on a background task. I'm using the bootstrap function to get the environment in place. One of the approaches I tried was:

from pyramid.plaster import bootstrap

with bootstrap(sys.argv[1]) as env
    dbsession = env['request'].dbsession

    with dbsession.begin_nested():
        res = dbsession.execute('''SELECT ....''')
        ...

That creates a SessionTransaction as expected, but still raises a NoTransaction.

How can I initialise the connection, so I can access it as I normally do in the views?

2 Answers 2

3

As described in https://github.com/Pylons/pyramid/issues/3219 the transaction is not initialised by default. It can be done using:

with bootstrap(sys.argv[1]) as env:
    with env['request'].tm:
        dbsession = env['request'].dbsession
        dbsession.execute(...)
Sign up to request clarification or add additional context in comments.

Comments

1

I've never used pyramid.plaster.bootstrap. However, you could use the same template as the script that is auto generated when you create a new project using the alchemy template.

pcreate -t alchemy myproject

The script looks like this:

import os
import sys
import transaction

from pyramid.paster import (
    get_appsettings,
    setup_logging,
    )

from pyramid.scripts.common import parse_vars

from ..models.meta import Base
from ..models import (
    get_engine,
    get_session_factory,
    get_tm_session,
    )
from ..models import MyModel


def usage(argv):
    cmd = os.path.basename(argv[0])
    print('usage: %s <config_uri> [var=value]\n'
          '(example: "%s development.ini")' % (cmd, cmd))
    sys.exit(1)


def main(argv=sys.argv):
    if len(argv) < 2:
        usage(argv)
    config_uri = argv[1]
    options = parse_vars(argv[2:])
    setup_logging(config_uri)
    settings = get_appsettings(config_uri, options=options)

    engine = get_engine(settings)
    Base.metadata.create_all(engine)

    session_factory = get_session_factory(engine)

    with transaction.manager:
        dbsession = get_tm_session(session_factory, transaction.manager)

        model = MyModel(name='one', value=1)
        dbsession.add(model)

And the entrypoints in setup.py looks like this:

entry_points="""\
  [paste.app_factory]
  main = myproject:main
  [console_scripts]
  initialize_myproject_db = myproject.scripts.initializedb:main
  """,

1 Comment

I spotted that script, but didn't feel great about the boilerplate code. I'll end up using it if necessary, but I'd like to use the simpler bootstrap if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.