Hey Jonathan,

I'm not going to touch the Python-Perl integration part :P

But for binding a instance and a type together, generally it's not recommended
to use the primary keys.  The reason for this is that while it will a) work
correctly eventually, and b) be stored correctly in the database, the ORM will
not automatically "refresh" new objects.  This means that though you build the
association correctly by setting the right primary key value in the right
foreign key slot, it won't have the object property populated as you'd
expected it to without 'refreshing' it explicitly, or clearing the cache
itself.

So try this syntax:

   myInst = Instance()
   myInst.name = 'random name'
   myInst.type = InstanceType.mapper.get(5) OR .select_by(name='FooType') OR
                 FooType # a type object you created previously.

Here we're setting the '.type' property to be a valid InstanceType object.
The ORM will automatically (or should!) set the appropriate foreign key values
in the appropriate places all behind the scenes.

The general idea is, once you've finished with creating the object, to leave
it in as fully populated a state as you'll expect to find it later, unless
you're going to do the objectstore.clear() route in which case you can rely on
it being fully populated the next time the mapper builds it from the backend
database.

Hope that helps!

Cheers,
-G

On Tuesday, April 25, 2006, 8:05:22 PM, you wrote:
> thanks a lot.

> i'm used to hand-coded sql.

> I've already the exact class / mapper structure that you suggested -  
> i figured that out based on the docs.

> I should have been a bit more clear on this point - I pretty much  
> understand the selecting and pulling data.
> And I understand the 'simple' inserts and joining 2 objects together.

> What I can't figure out though, is doing this sort of insert:

>         engine.begin()
>         myInstance = Instance()
>         Instance.name = 'random name'
>         Instance.type = ?
>         engine.commit()

> Where Instance.type is an id foreign keyed from
>         Instance.instance_type_id -> InsanceType.id

> To accomplish this would it just be
>         Instance.type_id = InstanceType.mapper.select_by(name= 'FOO')

> Or are there other ways?

> A lot of stuff i do is part-python and part-perl -- so I often store  
> about 200 id/name values that are really configuration items into the  
> database itself
> For stuff like this, i'm wondering if pre-caching these pairs is the  
> way to go.


> On Apr 25, 2006, at 1:37 PM, Gambit wrote:

>> Hey Jonathan,
>>
>> Welcome to ORMs :P
>>
>> I think your basic misunderstanding here is that with an ORM you're  
>> going to
>> be mapping columns -- potentially brought together from different  
>> tables --
>> into a single cohesive unit that represents a logical grouping,  
>> rather then
>> collapsing everything into a bucket.  Drawing from the examples in  
>> the docs,
>> you might have a "Users" class in Python that is -- and this is the  
>> cool part
>> -- automatically populated, committed, updated, deleted, etc. by  
>> the ORM
>> engine, in this case SQLAlchemy.
>>
>> So basically you don't deal with pre-caching your records anymore,  
>> for a
>> couple good reasons, two of which being pre-caching can be  
>> hideously expensive depending
>> on the size of the recordset, and second, it's often simpler to  
>> request
>> specific results when needed.
>>
>> Looking at your example code, I'd suspect something like the  
>> following may be
>> what you're looking for:
>>
>>    class Instance(object):
>>       pass
>>
>>    class InstanceType(object):
>>       pass
>>
>>    Instance.mapper = mapper(Instance, instance)
>>    InstanceType.mapper = mapper(InstanceType, instance_type)
>>
>>    # This could easily be rolled into the above Instance.mapper  
>> declaration, but
>>    # for transparency we'll keep it separate.
>>
>>    Instance.mapper.add_property('type', relation(InstanceType.mapper)
>>
>>    # Now that we have the class objects set up, we can access them  
>> like:
>>
>>    myins = Instance.mapper.get(5)   # Get the Instance with a  
>> primary key of 5
>>    print 'Instance number 5 is of type:', myins.type.name
>>
>>    # You could also search by instance name.  This produces the  
>> first Instance
>>    # that matches the name 'FOO'.
>>    myins = Instance.mapper.select_by(instance.c.name = 'FOO')[0]
>>
>> There's lots of other ways to do these things, and the  
>> documentation is fairly
>> extensive.  There's also a introduction/tutorial, state unknown at:
>>    http://www.sqlalchemy.org/trac/browser/sqlalchemy/trunk/doc/ 
>> build/content/tutorial.txt
>>
>> And, as always, irc://irc.freenode.net/#myghty where a few of us  
>> hang out :)
>>
> //Jonathan Vanasco

> |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
> - - - - - - - - - - - -
> | RoadSound.com / Indie-Rock.net
> | Collaborative Online Management And Syndication Tools
> |- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  
> - - - - - - - - - - - -





-------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to