Hey Lukasz,

theoretically declarative setup allowes autoload as well.
I have never tried it, but its in the SA docs.

It roughly goes like this:

http://www.sqlalchemy.org/docs/05/reference/ext/declarative.html?highlight=__table_args__#table-configuration

class MyClass(DeclarativeBase):
    __tablename__ = 'sometable'
    __table_args__ = (
            ForeignKeyConstraint(['id'], ['remote_table.id']),
            UniqueConstraint('foo'),
            {'autoload':True}
            )

Plus other things you might wanna add there.

Getting the python classes for 129 tables is a whole different story.
I personally would go with a class factory, but it feels kinda wrong.
I think youre moving into some area where it's not TG related, but SA
related. Probably you should trying reposting this on the SA mailing
list, Michael Bayer is usually there to answer tricky questions ;)

Greetings,

Tom



On Fri, Sep 18, 2009 at 4:10 PM, Lukasz Szybalski <[email protected]> wrote:
>
> On Fri, Sep 18, 2009 at 6:20 AM, Crusty <[email protected]> wrote:
>>
>> Alright there, second try Lukasz ;)
>>
>> the first thing you need to do, according to SA docs is having
>> metadata bound to a session:
>>
>> "To use autoload=True, the table's MetaData object need be bound to an
>> Engine or Connection, or alternatively the autoload_with=<some
>> connectable> argument can be passed."
>>
>> you can do this in 2 ways, first is like this:
>>
>>        
>> http://www.sqlalchemy.org/docs/05/metadata.html?highlight=metadata#binding-metadata-to-an-engine-or-connection
>>
>>        engine = create_engine('sqlite://', **kwargs)
>>
>>        # create MetaData
>>        meta = MetaData()
>>
>>        # bind to an engine
>>        meta.bind = engine
>>
>> and second is: you simply go with the autoload_with parameter, as
>> specified in the comments of /models/__init__.py:
>>
>>        t_reflected = Table("Reflected", metadata, autoload=True,
>> autoload_with=engine)
>>
>> According to the SA docs:
>>
>> http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/schema.html?highlight=table#sqlalchemy.schema.Table
>> Names which contain no upper case characters will be treated as case
>> insensitive names, and will not be quoted unless they are a reserved
>> word. Names with any number of upper case characters will be quoted
>> and sent exactly. Note that this behavior applies even for databases
>> which standardize upper case names as case insensitive such as Oracle.
>>
>> So "Reflected" is simply a case sensitive table name.
>>
>> My guess: like so many other TG things, the example would simply not
>> work as it is presented.
>>
>> You should be right about your question "where does Reflected come
>> from", because I think, it didnt come from anywhere, it would throw a
>> syntax error.
>> Just try writing:
>>
>>    class Reflected(objec): pass
>>
>>    t_reflected = Table("Reflected", metadata, autoload=True,
>> autoload_with=engine)
>>
>>    mapper(Reflected, t_reflected)
>>
>>
>
> What you are really saying is that you cannot autoload a table in any
> other file except for __init__.py
> You can define new tables but not autolaod them.
>
> Here is what finally works. How would I make this a declarative
> autoload? Since there is no examples I can't really tell.
>
> Before init_model:
> from sqlalchemy import Table
> from sqlalchemy.orm import mapper, relation
>
> Inside the init_model do:
>    recall_table = Table('recall_db', metadata,
> autoload=True,autoload_with=engine)
>
>    class Recall(object):
>        def __init__(self, **kw):
>            """automatically mapping attributes"""
>            for key, value in kw.iteritems():
>                setattr(self, key, value)
>
>    #Mapping of Table to Python Object Class
>    mapper(Recall, recall_table,primary_key=[recall_table.c.RECORD_ID])
>
>
> I'm not sure what the global portion of the example code is referring
> to? What is the declarative version?
>
>
> Question 2:
> How to autoload 129 tables in the database?
>
> #for name in engine.execute("SHOW TABLES"):
> #   tables[name] = sqlalchemy.Table(name, metadata, autoload=True)
>
> But how do I get the python class, and mappers for all these tables?
>
> Thanks,
> Lucas
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" 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/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to