On Jan 14, 2008, at 8:31 AM, Paul-Michael Agapow wrote:

>
> It seems there must be something I'm missing here, so hopefully  
> other sets of eyes will spot an obvious mistake.
>
> I have a series of objects that may-or-may not be persistent: some  
> will be created and stored to a db, some will be retrieved from the  
> db, maybe modified and updated, others will never go anywhere near  
> the db. (I have a series of programs using the same objects, only  
> some of which need to interact with the db.) However, I'm
>
> A simple test case. Given this code:
>
> class Sample (object):
>       def __init__ (self, id=None, title=None, description=None):
>               self.id = id
>               self.title = title
>               self.description = description
>
> class Conn (object):
>       def __init__ (self):    
>               conn_uri = CONN_URI_TMPL % TESTDB_CONNINFO
>               self.SA_ENGINE = create_engine (conn_uri)
>               self.SA_METADATA = MetaData (conn_uri)
>               self.SA_ENGINE.echo = True
>               Session = sessionmaker (bind=self.SA_ENGINE)
>               self.SA_SESSION = Session()
>               self.SA_QUERY = self.SA_SESSION.query
>
>               self.SAMPLETABLE = Table ('samples', self.SA_METADATA,
>                       Column ('id', Unicode(32), primary_key=True),
>                       Column ('title', Unicode(32)),
>                       Column ('description', Unicode(32)),
>               )
>               self.SA_METADATA.create_all (checkfirst=True)
>               clear_mappers()
>               mapper (Sample, self.SAMPLETABLE)
>
> Sample is the class I'd like to be able to persist if needed. Conn  
> just encapsulates the connection for test purposes. Now if I do this:
>
> c = Conn()
> s1 = Sample()
> s1.id = 'foo'
> c.SA_SESSION.save_or_update (s1)
> c.SA_SESSION.flush()
>
> all is well.  But if I create the Sample before the connection, it  
> doesn't work:
>
> s1 = Sample()
> s1.id = 'foo'
> c = Conn()
> c.SA_SESSION.save_or_update (s1)
> c.SA_SESSION.flush()
>
> ....
> AttributeError: 'ColumnProperty' object has no attribute 'key'
>
> So, how can I persist an object constructed before the connection is  
> established? Or is it necessary to do all work either within or  
> without of the context of a session?
>
>

the mappers cannot compile properly without the Table objects existing  
and being fully initialized.  Similarly, instantiating an object  
before its mapper is defined will lead to poor results.    So  
construct your MetaData and Tables before you construct your mappers,  
and construct your mappers before you instantiate mapped objects.   
Connections and engines on the other hand are entirely decoupled from  
all of that and dont need to be established until needed (i.e. right  
before the flush()).




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to