On Thu, Oct 16, 2008 at 2:08 AM, Mike Orr <[EMAIL PROTECTED]> wrote:
>
> 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]>
OK. I have clean Pylons 0.9.6.2 app working with 3 classes into
separate modules.
Works with MySql. Sqlite seems not to store objects (code the same,
just exchanging sqlalchemy.url). Why?
Cyclic dependencies do not work just that simple. "Cyclic" imports
unfortunately have to placed inside a method - not at the top of
module.
I'd still use some help here.
I'll try to attach full source - hope googlegroups allows that..
For impatient sample code below:
app=pycon, requires mysql or sqlite, 3 classes defined
#############################################
pycon.model.phone.py:
class Phone(object):
pass
pycon.model.participant.py
from pycon.model.conference import Conference
class Participant(object):
pass
pycon.model.conference.py:
from datetime import datetime
class Conference(object):
def __init__(self, subject=None):
self.subject = subject
self.date = datetime.now()
def __str__(self):
return "%s, s: %s" % (self.__class__.__name__, self.subject)
def cyclic_dependency(self):
#importing at the top of module causes cyclic import issue :(
from pycon.model.participant import Participant
pass
pycon.model.__init__.py:
import sqlalchemy as sa
from sqlalchemy import orm
from pycon.model import meta
from pycon.model.phone import Phone
from pycon.model.participant import Participant
from pycon.model.conference import Conference
def init_model(engine):
"""Call me before using any of the tables or classes in the model."""
sm = orm.sessionmaker(autoflush=True, transactional=True, bind=engine)
meta.engine = engine
meta.bind = engine
meta.Session = orm.scoped_session(sm)
t_phone = sa.Table("phone", meta.metadata, autoload=True,
autoload_with=engine)
t_participant = sa.Table("participant", meta.metadata,
autoload=True, autoload_with=engine)
t_conference = sa.Table("conference", meta.metadata,
autoload=True, autoload_with=engine)
orm.mapper(Phone, t_phone)
orm.mapper(Participant, t_participant)
orm.mapper(Conference, t_conference, properties = {
'participants' : orm.relation(Participant,
primaryjoin=t_conference.c.id==t_participant.c.conference_id,
foreign_keys=t_participant.c.conference_id
)
})
pycon.controllers.conference.py:
from pycon.lib.base import *
from pycon.model.participant import Participant
from pycon.model.conference import Conference
from pycon.model.phone import Phone
from pycon.model.meta import Session
class ConferenceController(BaseController):
def index(self):
conf = Conference(subject='test')
Session.save_or_update(conf)
return "Conferences: %d" % len(Session.query(Conference).all())
..lib.base.py and
..websetup.py and
..config.environment.py are essentialy the same as in the tutorial
--
_i______'simplicity_is_the_key'__________tomasz_nazar
_ii____'i_am_concern_oriented'________________JKM-UPR
_iii__'patsystem.sf.net'___________________linux_user
_'aspectized.com'___________________________prevayler
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---