Michael!

Thank you for such an excellent and prompt reply!  I'm going to use
method two.  It looks very convenient.

Thanks again---
Sam

P.S. Perhaps an example of method two could be added to the tutorial
for the next person in my predicament? :)


On Sep 5, 7:19 am, Michael Bayer <[EMAIL PROTECTED]> wrote:
> On Sep 5, 2008, at 12:23 AM, Sam wrote:
>
>
>
> > Michael...
>
> > I've read the tutorial, and I think I understand it.
>
> > But maybe I don't understand reflection.  I thought that I could use
> > it to avoid defining anything.  I'd like to have objects basically
> > spring into life knowing exactly what their row names are, without
> > having to type anything.
>
> Sam -
>
> you have the reflection part down.  Now you have a Table object, all  
> the columns are there, and you don't need to restate them in any  
> case.    I'll lay out three different approaches to using it.
>
> First is, classical ORM usage.   For this, the Table has to be mapped  
> to a class.    this would look like:
>
> class MPhone(object):
>      def __init__(self, **kw):
>          for k in kw:
>              setattr(self, k, kw[k])
>
> mapper(MPhone, mhones)
>
> Second, a shortcut to the above approach is to use the declarative  
> module.  You can have a less verbose approach and you'll get the  
> MPhone(x='x', y='y', ...) constructor you're looking for for free  
> (i.e. without needing the above __init__):
>
> from sqlalchemy import *
> from sqlalchemy.orm import sessionmaker
> from sqlalchemy.ext.declarative import declarative_base
>
> engine = create_engine("postgres://postgres:[redacted]/mydb")
> Base = declarative_base(engine=engine)
>
> class MPhone(Base):
>      __tablename__ = 'mphones'
>      __table_args__= {'autoload':True}
>
> Session = sessionmaker()
> sess = Session()
>
> sess.add(MPhone(word='foo', mphone='bar'))
> sess.commit()
>
> Third approach, don't use the ORM, just use the Table and create an  
> INSERT statement:
>
> from sqlalchemy import *
>
> engine = create_engine("postgres://postgres:[redacted]/mydb")
> meta = MetaData()
> meta.bind = engine
>
> mphones = Table('mphones', meta, autoload=True)
> mphones.insert().execute(word='foo', mphone='bar')
--~--~---------~--~----~------------~-------~--~----~
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