Quickest way to use Elixir with a Pylons 0.9.6 project.

15 views
Skip to first unread message

Tim Riley

unread,
Sep 27, 2007, 9:19:30 AM9/27/07
to pylons-...@googlegroups.com
I was looking at "SQLAlchemy 0.4 for people in a hurry" and it seems
the Elixir section isn't completed yet. I also looked at Tesla but it
uses sacontent which according to the site is depreciated and points
to the SQLAlchemy for people in a hurry section of the pylons wiki.

With that being said I was wondering if someone could throw me some
pointers on getting the SVN version of Elixir working with SQLAlchemy
0.4 and pylons. Any pointers would be greatly appreciated.

Note:
I realize that SQLAlchemy is awesome and I should just code my models
directly in it instead of Elixir. However I'd prefer to use Elixir.

Isaac Csandl

unread,
Sep 27, 2007, 1:39:37 PM9/27/07
to pylons-discuss
I advise not using the SVN version of Elixir just yet. It's still a
little funky as of a few days ago, and some big changes are in the
works. Stick with the 0.3 release, or try rev 174 if you want to use
some of the recent extensions.

I was able to get a 0.9.6 project going with Tesla, but there is a bug
in Tesla where you need to add:

from <yourpackagename>.config.middleware import make_app

... to your project's __init__.py before anything will work.

You'll get a few deprecation warnings which include tips on what to
update, but it works.

HTH
--Isaac


Jonathan LaCour

unread,
Sep 27, 2007, 1:59:13 PM9/27/07
to pylons-...@googlegroups.com
Tim Riley wrote:

> With that being said I was wondering if someone could throw me some
> pointers on getting the SVN version of Elixir working with SQLAlchemy
> 0.4 and pylons. Any pointers would be greatly appreciated.

I am using the SVN version of Elixir and SQLAlchemy in a Pylons project,
and its pretty straightforward. In my model.py:

import elixir

Session = elixir.session
metadata = elixir.metadata

In my config/environment.py at the bottom of load_environment:

from sqlalchemy import engine_from_config
from myproject import model
import elixir

config['pylons.g'].sa_engine = engine_from_config(config,
'sqlalchemy.')
model.Session.bind=config['pylons.g'].sa_engine
model.metadata.bind=config['pylons.g'].sa_engine

elixir.setup_all()

Thats it. I also have a simple piece of WSGI middleware that wraps
each request in a transaction automatically, if it isn't a GET or HEAD
request. Note that this may change if we make changes in Elixir SVN
before release, but it works for me right now.

> I realize that SQLAlchemy is awesome and I should just code my models
> directly in it instead of Elixir. However I'd prefer to use Elixir.

You shouldn't feel bad about this. It's your choice! We're trying to
improve our documentation for the upcoming Elixir 0.4, along with adding
some additional utilities to help users to see what is going on under
the hood. Hopefully once we get this release out the door, it'll be
much simpler to integrate Elixir into your Pylons project if you want
to.

Best of luck -

--
Jonathan LaCour
http://cleverdevil.org


Tim Riley

unread,
Sep 27, 2007, 4:17:05 PM9/27/07
to pylons-...@googlegroups.com
Thank you Jonathan, that seems to be exactly what I was looking for.
Once I mess around with this some more I will update the SQLAlchemy
0.4 for people in a hurry.

Isaac Csandl

unread,
Sep 28, 2007, 11:43:17 AM9/28/07
to pylons-discuss
D'oh!

There was a problem in my model that didn't bother the older Elixir
but it did bother the latest version... I mistook it for a problem
with the latest version and not my code. Odd, but there you have it.

Thanks for clearing up how easy it is to get it going without
Tesla. :D

Also, from the SA 0.4 for people in a hurry doc, add this to
websetup.py at the end of setup_config:

from myproject import model
print "Creating tables"
model.metadata.create_all(bind=config['pylons.g'].sa_engine)
print "Successfully setup"


--i

Message has been deleted
Message has been deleted

Daniele Paolella

unread,
Oct 1, 2007, 5:51:40 AM10/1/07
to pylons-discuss
On 27 Set, 19:59, Jonathan LaCour <jonathan-li...@cleverdevil.org>
wrote:

>
> import elixir
>
> Session = elixir.session
> metadata = elixir.metadata
>

I tried this with Elixir revision 216 and SA 0.4.0 beta 6, but I got
breaks here and there.
Since Session.commit() won't work with non-transactional Session, I
tried upgrading configuration:

>>> Session.configure(autoflush=True, transactional=True)
<type 'exceptions.AttributeError'>: 'function' object has no attribute
'configure'

no luck, then obtaining a non default Session:

>>> from sqlalchemy.orm import scoped_session, sessionmaker
>>> Session = scoped_session(sessionmaker(autoflush=True, transactional=True))

ellipsis on binding; let's test it:

>>> class Employee(elixir.Entity):
... elixir.using_options(autoload=True)
>>> me = Employee(name='paul')
>>> me.name
'paul'
>>> Session.commit()
>>> me.id

that gave nothing, no insertion was committed; this config also broke
all my model tests;
I ended with:

>>> Session.commit = Session.flush

to avoid replacing all my commit()s. Now app is working but no
transactions.
Do you guys all stick with objectstore? What I missed?

Jonathan LaCour

unread,
Oct 1, 2007, 8:54:27 AM10/1/07
to pylons-...@googlegroups.com
Daniele Paolella wrote:

> I tried this with Elixir revision 216 and SA 0.4.0 beta 6, but I
> got breaks here and there. Since Session.commit() won't work with
> non-transactional Session, I tried upgrading configuration:

Sorry, I didn't take a close look at my project before typing up that
last message. You should basically follow the SQLAlchemy 0.4.0 in a
Hurry recipe from the wiki, except in your model.py, you should have:

from sqlalchemy.orm import scoped_session, sessionmaker

import elixir

Session = scoped_session(sessionmaker(autoflush=True,
transactional=True))

elixir.session = Session
metadata = elixir.metadata

This will make sure that you override the default elixir session with a
transactional, auto-flushing session, just like the recipe recommends.
In order to have your objects saved, just call Session.commit(). I do
this on every POST request in a small WSGI middleware if there were no
exceptions, but you can also do it manually after any update/create
occurs, or in your base controller.

If its still not working, let us know over on the Elixir mailing list,
and maybe we can help you over there.

Rav

unread,
Nov 5, 2007, 9:37:29 AM11/5/07
to pylons-discuss
Thanks for help, but when I try to use models in controllers I receive
empty lists.Should I really try to write for example list =
model.MyModel.query.all() in controllers' functions?

Reply all
Reply to author
Forward
0 new messages