title: How do I tell sqlalchemy to ignore certain (say, null) columns on INSERT
body: <p>I have a legacy database that creates default values for several columns using a variety of stored procedures. It would be more or less prohibitive to try and track down the names and add queries to my code, not to mention a maintenance nightmare.</p>
  
  <p>What I would <em>like</em> is to be able to tell sqlalchemy to ignore the columns that I don't really care about. Unfortunately, it doesn't. Instead it provides <code>null</code> values that violate the DB constraints.</p>
  
  <p>Here's an example of what I mean:</p>
  
  <pre><code>import sqlalchemy as sa
  import logging
  from sqlalchemy.orm import sessionmaker
  from sqlalchemy.ext.declarative import declarative_base
  
  l = logging.getLogger('sqlalchemy.engine')
  l.setLevel(logging.INFO)
  l.addHandler(logging.StreamHandler())
  
  
  
  
  engine = sa.create_engine('postgresql+psycopg2://user@host:port/dbname')
  
  Session = sessionmaker(bind=engine)
  session = Session()
  temp_metadata = sa.MetaData(schema='pg_temp')
  TempBase = declarative_base(metadata=temp_metadata)
  
  with session.begin(subtransactions=True):
      session.execute('''
          CREATE TABLE pg_temp.whatevs (
              id      serial
            , fnord   text not null default 'fnord'
            , value   text not null
          );
  
          INSERT INTO pg_temp.whatevs (value) VALUES ('something cool');
      ''')
  
      class Whatever(TempBase):
          __tablename__ = 'whatevs'
  
          id = sa.Column('id', sa.Integer, primary_key=True, autoincrement=True)
          fnord = sa.Column('fnord', sa.String)
          value = sa.Column('value', sa.String)
  
      w = Whatever(value='something cool')
      session.add(w)
  </code></pre>
  
  <p>This barfs, because:</p>
  
  <pre><code>INSERT INTO pg_temp.whatevs (fnord, value) VALUES (%(fnord)s, %(value)s) RETURNING pg_temp.whatevs.id
  {'fnord': None, 'value': 'something cool'}
  ROLLBACK
  Traceback (most recent call last):
    File "/home/wayne/.virtualenvs/myenv/lib64/python3.5/site-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
      context)
    File "/home/wayne/.virtualenvs/myenv/lib64/python3.5/site-packages/sqlalchemy/engine/default.py", line 450, in do_execute
      cursor.execute(statement, parameters)
  psycopg2.IntegrityError: null value in column "fnord" violates not-null constraint
  DETAIL:  Failing row contains (2, null, something cool).
  </code></pre>
  
  <p>What I <em>expected</em> is that it would just skip out on the <code>fnord</code> column, since it didn't get set.</p>
  
  <p>Even if I do:</p>
  
  <pre><code>w = Whatever()
  w.value = 'this breaks too'
  </code></pre>
  
  <p>or add:</p>
  
  <pre><code>def __init__(self, value):
      self.value = value
  </code></pre>
  
  <p>to the <code>Whatever</code> class... still no dice.</p>
  
  <p>How can I tell sqlalchemy that "look, these other columns are fine, I know I'm not providing a value - the database is going to take care of that for me. It's okay, just don't worry about these columns"? </p>
  
  <p>The only way I'm aware of is to futz with the class definition and lie, saying those columns don't exist... but I <em>do</em> actually want them to come in on queries.</p>