[sqlalchemy] Clearing an already defined mapper

2011-01-24 Thread Massi
Hi everyone, in my script I have to deal with a table whose number of
columns can change at runtime. Since I have no information about the
structure I will have to handle at a given moment, I would need to
change dynamically the mapping during the program. If I try to re-map
the table I obviously get the following error:

sqlalchemy.exc.ArgumentError: Class 'class 'MyMappingClassa''
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.

On the other hand clear_mappers removes all the mappers that has been
defined on the classes; furthermore, as the documentation says:

clear_mappers` is *not* for normal use, as there is literally no valid
usage for it outside of very specific testing scenarios

Can anyone give a hint to handle correctly this situation or point me
out if there is a better approach to face it?

Thanks in advance.

-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.



Re: [sqlalchemy] Clearing an already defined mapper

2011-01-24 Thread Michael Bayer

On Jan 24, 2011, at 6:55 AM, Massi wrote:

 Hi everyone, in my script I have to deal with a table whose number of
 columns can change at runtime. Since I have no information about the
 structure I will have to handle at a given moment, I would need to
 change dynamically the mapping during the program. If I try to re-map
 the table I obviously get the following error:
 
 sqlalchemy.exc.ArgumentError: Class 'class 'MyMappingClassa''
 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.
 
 On the other hand clear_mappers removes all the mappers that has been
 defined on the classes; furthermore, as the documentation says:
 
 clear_mappers` is *not* for normal use, as there is literally no valid
 usage for it outside of very specific testing scenarios
 
 Can anyone give a hint to handle correctly this situation or point me
 out if there is a better approach to face it?

First I'll note that if the use case here is a table where some human or 
process is constantly adding/dropping columns and the application needs to just 
keep running and auto-adjusting to the schema changes, that is completely the 
wrong way to use relational databases.  Its like renovating your kitchen as you 
make dinner at the same time.

Secondly, assuming that use case, you want to create the classes themselves on 
the fly.  Use the recipe at entity name:  
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/EntityName


-- 
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 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.