On Wed, Oct 15, 2008 at 4:35 PM, Tomasz Nazar <[EMAIL PROTECTED]> wrote: > > On Tue, Oct 14, 2008 at 5:20 AM, Michael Bayer <[EMAIL PROTECTED]> wrote: >> >> >> On Oct 13, 12:14 pm, "Tomasz Nazar" <[EMAIL PROTECTED]> wrote: >>> 1) all model classes are defined in 1 file together with database mapping >> >> like everyone is saying, you can roll this however you want and >> however is appropriate to the type of app you're building. > > > Michael and Mike - I've just found your "new" tutorial on how to use > SQLA and how to setup classes in separate files! That's it! > I wasn't looking into docs for a while :( > http://wiki.pylonshq.com/display/pylonsdocs/Using+SQLAlchemy+with+Pylons > > It looks I can split my big file - I dreamed about that. That is great! > > One stuff that doesn't work for me is "autoload" a table definition. I > almost copied your MyApp.tar.gz sources and adjusted to autoload. But > doesn't work. > > --> sqlalchemy.exceptions.UnboundExecutionError: The MetaData is not > bound to an Engine or Connection. Execution can not proceed without a > database to execute against. Either execute with an explicit > connection or assign the MetaData's .bind to enable implicit > execution. > > I tried to move all mappings + t_tables to init_model (as engine is > only available then), but still fail. > Is it too much asking for similar source code, but with classes split > into files and working with 'autoload'? > Or point to a right way at least :) I can upload source when I'm done with > it..
I think you need to read the SQLAlchemy manual about how engines propagate and autoloading works. Then you'll be able to do it yourself. In summary, SQL queries (including ORM operations) need a database connection on which to operate. The engine gives out connections in a thread-safe manner, so you can just pretend the engine is a connection and everything works. The engine is necessary only when you *execute* a query; e.g., SQL.execute(), Session.execute(), Session.commit(). query.all(), using a query as an iterator, etc. Most execution methods take a bind= argument to make it use that engine. Autoloading is an exception because its argument is autoload_with= . (I think it was a bad design choice to not call it bind=, but that's the way it is.) If you don't use bind=, a Session-based query will look for an engine bound to the session, and then for an engine bound to the metadata. If neither exists the query will fail with the error you're seeing. A non-session-based SQL query will look in the metadata for an engine, and fail in the same way if there is none. So you should bind your engine to the session, use Session.execute() for any non-ORM operations you have to do, and pass bind= and autoload_with= to the remaining methods which don't fit into either category: table(autoload_with=), meta.create_all(bind=). You can also bind the engine to the metadata to avoid the inconvenience of those bind= and autoload_with= arguments. Bound metadata is no longer recommended for beginners because it's a bit magical, and because you can get into trouble if you have an ORM transaction open and simultaneously modify the database at the SQL level without going through Session.execute() -- this may give the ORM an incorrect impression of the database state, or the database may implicitly commit the pending transaction without the ORM realizing it. But it does work. -- Mike Orr <[EMAIL PROTECTED]> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to pylons-discuss@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en -~----------~----~----~----~------~----~------~--~---