On Jul 1, 2011, at 8:11 AM, Oltmans wrote:
> Greetings,
>
> First, many thanks for creating such an awesome library. Secondly, I'm
> very new to this and I've a basic question. I've a method that looks
> like following
>
> def test_table_load(self):
> doctor = Table('TestTable', META, autoload=True, autoload_with=DB)
>
>
> Now while the software runs, this method is called probably 40--50
> times.
> I'm using MySQL and have following questions
>
> - So if I call test_table_load() 10 times, SQLAlchemy will load the
> database table with all records 10 times?
"autoload" only loads information about the table itself, i.e. the names and
types of columns, and constraints. It doesn't load any rows.
The above function will in fact emit the autoload operation only once - on
subsequent calls, 'TestTable' will be present in META and it is returned as is.
That said, you'd be better off declaring "doctor" just once, somewhere in the
module level (or in a function that is called only once) once DB is established:
def load_all_tables(DB):
global doctor, patient, hospital
doctor = Table('doctor', META, autoload=True, autoload_with=DB)
patient = Table('patient', META, autoload=True, autoload_with=DB)
hospital = Table('hospital', META, autoload=True, autoload_with=DB)
# later...
DB = create_engine('...')
load_all_tables(DB)
> - If I want to call test_table_load() 10 times but I want to make sure
> SQLAlchemy loads 'TestTable' only once in memory should I replace this
> doctor = Table('Doctor', META, autoload=True, autoload_with=DB)
> with
> doctor = Table('Doctor', META, autoload=True, autoload_with=DB,
> keep_existing=True)
keep_existing is a flag that indicates if you had other information in Table,
i.e. more Column objects or similar, that they should be ignored. Its so
that you can say:
def _define_table(self):
return Table('sometable', metadata, Column('x', Integer),
keep_existing=True)
and if your application runs in such a way that _define_table() gets called
twice, the resulting Table, which remember is the same Table object both times,
will only use Column('x') the first time, it wont replace Column('x') with
another Column('x'), or in the case of a constraint add a redundant constraint,
the second time it is called.
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" 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/sqlalchemy?hl=en.