On Oct 18, 2010, at 5:36 AM, Faheem Mitha wrote: > > Hi, > > I should say in advance that I don't have a reproduction script for this, > though I could probably manufacture one if necessary. > > I have a python script which calls a function repeatedly. Each time the > function is called, it creates a bunch of SQLA tables, classes and mappers > between them. I'd be perfectly happy to remove these after each invocation, > but I don't know how, and apparently they stick around. I'd hope that these > objects all get overwritten. Since the classes are just regular Python > classes, I know these do get overwritten. I'm not sure about the Table > objects, and it definitely appears that the mappers don't get cleanly > overwritten, judging by the error message I get (complete traceback at the > bottom of the message) > > sqlalchemy.exc.ArgumentError: Class '<class 'dbschema.Pheno'>' already has a > primary mapper defined. Use non_primary=True to create a non primary Mapper. > clear_mappers() will remove *all* current mappers from all classes.
> > So, I want to > > a) confirm that the Table objects get overwritten (they contain state data, > correct?) and I don't want the Table objects from a previous invocation to > get mixed up with one from a later invocation. Table objects don't get "overwritten". They are created exactly once for a specific MetaData object, and an error is raised if you attempt to make a new Table of the same name within the same MetaData object. If you'd like to make all new tables, use a new MetaData object. As far as "magic", we are talking here about a dictionary of name->Table object where a check for "if name in dictionary: raise error" is performed for the MetaData object in question. > > and ask > > b) why aren't the mappers overwritten? Similarly, mappers don't "overwrite" previously existing mappers on a certain class. They're a one-time operation on a specific class until clear_mappers() is called. Based on the description of the problem above, you need to ensure your Python classes are declared in the same scope as your mappers. The error message you're getting suggests that 'Pheno' is declared at the module level. Again, as far as "magic", we are talking about a dictionary of class->mapper object where a check for "if class in dictionary: raise error" is performed when mapper() is called for a particular class. > > I'm using clear_mappers as a temporary measure. as suggested by the error > message, but I don't know if this the right approach. It does get rid of the > error message. Clarifications appreciated. I apologise if this message comes > across as "computationally illiterate" but despite using SQLA for nearly two > years (has it been so long?), I still have little understanding of how some > of it's magic is accomplished. Thanks! > > Regards, Faheem > > *********************************************************************** > traceback follows > *********************************************************************** > > Traceback (most recent call last): > File "load_all_datasets.py", line 58, in <module> > load_all_datasets(dbname, dbtype, options.test, options.shard, > options.recreate, options.jobs) > File "/home/faheem/snppy/utils.py", line 6, in wrapper > res = func(*args, **kwargs) > File "load_all_datasets.py", line 39, in load_all_datasets > load_dataset(schema, dbname, options.alleletype, options.jobs, > options.recreate, options.shard, options.test) > File "/home/faheem/snppy/load_dataset.py", line 71, in load_dataset > p = platform.Single_Illumina(phenofiles, annotfile, genofile, > genotablefile, newschema, alleletype, dbtype, dbstring_dbname, jobs) > File "/home/faheem/snppy/platform.py", line 130, in __init__ > table_dict = make_tables_illumina(schema) > File "/home/faheem/snppy/dbschema.py", line 293, in make_tables > 'race':relation(Race, backref='patients'), > File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/__init__.py", line 751, in > mapper > return Mapper(class_, local_table, *args, **params) > File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/mapper.py", line 197, in > __init__ > self._configure_class_instrumentation() > File "/usr/lib/pymodules/python2.5/sqlalchemy/orm/mapper.py", line 349, in > _configure_class_instrumentation > self.class_) > sqlalchemy.exc.ArgumentError: Class '<class 'dbschema.Pheno'>' already has a > primary mapper defined. Use non_primary=True to create a non primary Mapper. > clear_mappers() will remove *all* current mappers from all classes. > > -- > 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. > -- 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.
