title: sqlalchemy dynamic mapping
body: <p>I have the following problem:</p>
  
  <p>I have the class:</p>
  
  <pre><code>
  class Word(object):
  
      def __init__(self):
          self.id = None
          self.columns = {}
  
      def __str__(self):
          return "(%s, %s)" % (str(self.id), str(self.columns))
  </code></pre> 
  
  <p>self.columns is a dict which will hold (columnName:columnValue) values. The name of the columns are known at runtime and they are loaded in a wordColumns list, for example </p>
  
  <pre><code>
  wordColumns = ['english', 'korean', 'romanian']
  </code></pre>
  
  <pre><code>
  wordTable = Table('word', metadata,
                    Column('id', Integer, primary_key = True)
                    )
  for columnName in wordColumns:
      wordTable.append_column(Column(columnName, String(255), nullable = False))
  </code></pre>
  
  <p>I even created a explicit mapper properties to "force" the table columns to be mapped on word.columns[columnName], instead of word.columnName, I don't get any error on mapping, but it seems that doesn't work. </p>
  
  <pre><code>
  mapperProperties = {}
  for column in wordColumns:
      mapperProperties['columns[\'%']' % column] = wordTable.columns[column]
  
  mapper(Word, wordTable, mapperProperties)
  </code></pre>
  
  <p>When I load a word object, SQLAlchemy creates an object which has the word.columns['english'], word.columns['korean'] etc. properties instead of loading them into word.columns dict. So for each column, it creates a new property. Moreover word.columns dictionary doesn't even exists.</p>
  
  <p>The same way, when I try to persist a word, SQLAlchemy expects to find the column values
  in properties named like word.columns['english'] (string type) instead of the dictionary word.columns.</p>
  
  <p>I have to say that my experience with Python and SQLAlchemy is quite limited, maybe it isn't possible to do what I'm trying to do.</p>
  
  <p>Any help appreciated,</p>
  
  <p>Thanks in advance.   </p>