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.
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
> 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
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
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?
> 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.