Re: [sqlalchemy] Using reflection to create read-only classes and objects

2010-03-26 Thread Michael Bayer
mr wrote:
> Hejhej,
> first I'd like to thank the developers of SQLAlchemy for their great
> effort. The library works like a treat and is well documented. It's a
> pleasure working with it...
> Nevertheless I've currently run into a problem when trying to create
> classes that are based on read-only reflection from existing database
> tables. Here *read-only* means that I have a database table with fixed
> contents that I'm not going to change in application. However, I need
> this table and its contents to create other objects.
> Let's assume we have a table *foos* with a number of rows - each of
> them representing a distinct *foo*. There is the usual id column
> *foo_id*, then there's *foo_name* and *foo_key*. I've been able to
> come up with the following code to that uses reflection to derive the
> table metadata.
>
> engine = create_engine('postgresql://...')
> Base = declarative_base()
>
> class Foo(Base):
> __tablename__ = 'foos
> __autoload__ = True
> __table_args__ = {'autoload_with': engine}
>
> def __init__(self):
> pass
>
> Now when I create a foo object with f = Foo() I can see all columns of
> the existing table by doing something like:
> print f.metadata.tables[f.__tablename__].columns
>
> What I want to do now is to initialize a *foo*-object by specifying
> *foo_key* in the constructor so that I have exactly the one distinct
> row containing *foo_key* at my disposal for further processing.
>
> def __init__(self, foo_key):
> ...
>
> However I was not able to do this... Am I missing something here? Or
> did anybody else had a similar problem and was able to solve it...
> Thank you very much in advance.

I think you are looking for this 
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/UniqueObject




> Regards,
> Markus
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
>

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



[sqlalchemy] Using reflection to create read-only classes and objects

2010-03-26 Thread mr
Hejhej,
first I'd like to thank the developers of SQLAlchemy for their great
effort. The library works like a treat and is well documented. It's a
pleasure working with it...
Nevertheless I've currently run into a problem when trying to create
classes that are based on read-only reflection from existing database
tables. Here *read-only* means that I have a database table with fixed
contents that I'm not going to change in application. However, I need
this table and its contents to create other objects.
Let's assume we have a table *foos* with a number of rows - each of
them representing a distinct *foo*. There is the usual id column
*foo_id*, then there's *foo_name* and *foo_key*. I've been able to
come up with the following code to that uses reflection to derive the
table metadata.

engine = create_engine('postgresql://...')
Base = declarative_base()

class Foo(Base):
__tablename__ = 'foos
__autoload__ = True
__table_args__ = {'autoload_with': engine}

def __init__(self):
pass

Now when I create a foo object with f = Foo() I can see all columns of
the existing table by doing something like:
print f.metadata.tables[f.__tablename__].columns

What I want to do now is to initialize a *foo*-object by specifying
*foo_key* in the constructor so that I have exactly the one distinct
row containing *foo_key* at my disposal for further processing.

def __init__(self, foo_key):
...

However I was not able to do this... Am I missing something here? Or
did anybody else had a similar problem and was able to solve it...
Thank you very much in advance.
Regards,
Markus

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