SQLAlchemy create_engine future argument

70 views
Skip to first unread message

Nate J

unread,
Jul 27, 2021, 6:24:11 PM7/27/21
to pylons-...@googlegroups.com
Hi List,

How should I set the the future argument to True in the call to create_engine()?

The Engine is created by using create_engine(), specifying the create_engine.future flag set to True so that we make full use of 2.0 style usage:
>>> from sqlalchemy import create_engine
>>> engine = create_engine("sqlite+pysqlite:///:memory:", echo=True, future=True)

What I’ve tried that doesn’t appear to be working is adding it to settings in myproject/__init__.py:main():

def main(global_config, **settings):
    """Return a Pyramid WSGI application."""
    settings['sqlalchemy.future'] = True
    ...

Later, after starting Pyramid, I see it passed to create_engine() in my debugger.

In sqlalchemy.engine.create.py:engine_from_config():
options is: {'future': True, '_coerce_config': True}
And then:
return create_engine(url, **options)

However, if I look at its value in a class’ view, I see future is False, i.e.:

def __init__(self, request):
    self.request = request

@view_config(route_name=’test', renderer=‘/test.mako')
def test(self):
    # From here, self.request.dbsession.future is False.
    …

I presume it should be True at this point.

Has anyone seen this or set this flag and used it successfully?

Thanks

Theron Luhn

unread,
Jul 27, 2021, 6:41:11 PM7/27/21
to pylons-...@googlegroups.com
It looks like the `future` flag on Engine and the `future` flag on Session are two separate things.  So you need to add `future=True` to either sessionmaker or the Session constructor.

— Theron



--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/48FCE954-14C5-4695-92EC-9CB8582ED990%40gmail.com.

Nathan Jennings

unread,
Jul 29, 2021, 10:58:26 PM7/29/21
to pylons-discuss
Thanks for your help. I really appreciate it.

After looking at this some more, self.request.dbsession.future is always False regardless of what I've tested.

However, I did find self.request.dbsession.bind._is_future.

If I use the code below in __init__.py:main() before settings is passed to Configurator():

settings['sqlalchemy.future'] = True

then in one of my view methods, self.request.dbsession.bind._is_future is True. Otherwise, self.request.dbsession.bind._is_future is False.

So, if this is the correct flag SQLAlchemy sets/uses, it appears to be making the trip all the way into the view, which is what I wanted to validate.

Jonathan Vanasco

unread,
Aug 13, 2021, 1:14:50 PM8/13/21
to pylons-discuss
> It looks like the `future` flag on Engine and the `future` flag on Session are two separate things.  So you need to add `future=True` to either sessionmaker or the Session constructor.

Confirming this.  There is also a `future` on `Connection` objects, but that is inherited from `Engine`.  Session and Engine/Connection are entirely unrelated.  Please see:

> However, I did find self.request.dbsession.bind._is_future.

that is `Session.bind`, which will be an `Engine` or `Connection` object - not the Session itself.
 
If I use the code below in __init__.py:main() before settings is passed to Configurator():

settings['sqlalchemy.future'] = True


So, if this is the correct flag SQLAlchemy sets/uses, it appears to be making the trip all the way into the view, which is what I wanted to validate.

Your Engine is in future mode, but your Session is not.  As Theron noted, you need to change your sessionmaker construction.  If you are using the cookiecutter setup, this is what must be adjusted:

https://github.com/Pylons/pyramid-cookiecutter-starter/blob/latest/%7B%7Bcookiecutter.repo_name%7D%7D/%7B%7Bcookiecutter.repo_name%7D%7D/sqlalchemy_models/__init__.py#L19-L22

 
 

Nate J

unread,
Sep 1, 2021, 11:23:33 AM9/1/21
to pylons-...@googlegroups.com
Thanks for confirming this and all the info and links. I really appreciate it. It’s a big help.

Thank you too Theron for your help. I hadn’t quite made it that far in the docs yet, but I’ve had a chance to look at it again.

Thank you both!

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages