title: Error on db.session.commit() With sqlachemy and Oracle
body: <p>I'm following the Flask Mega Tutorial by Miguel here <a href="http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database" rel="nofollow">http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database</a> with just a small modification as using Oracle instead of sqlite as my db. I'm also using flask migrate instead of SQLAlchemy-migrate. </p>
  
  <p>This is my models.py</p>
  
  <pre><code>from app import db
  
  class User(db.Model):
  
      id = db.Column(db.Integer,db.Sequence('u_id'), primary_key=True)
  
      nickname = db.Column(db.String(64), index=True, unique=True)
  
      email = db.Column(db.String(120), index=True, unique=True)
  
      posts = db.relationship('Post', backref='author', lazy='dynamic')
  
      def __repr__(self):
          return '&lt;User %r&gt;' % (self.nickname)
  
  class Post(db.Model):
  
      id = db.Column(db.Integer, primary_key = True)
  
      body = db.Column(db.String(140))
  
      timestamp = db.Column(db.DateTime)
  
      user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
  
      def __repr__(self):
          return '&lt;Post %r&gt;' % (self.body)
  </code></pre>
  
  <p>According the the sqlachemy docs here <a href="http://docs.sqlalchemy.org/en/latest/dialects/oracle.html" rel="nofollow">http://docs.sqlalchemy.org/en/latest/dialects/oracle.html</a>, since oracle doesn't support autoincrement, I have to specify a sequence. I have done that in the id column definition.</p>
  
  <p>On the python prompt, I'm able to do:</p>
  
  <pre><code>from app import db, models
  
  u = models.User(nickname='john', email='john@email.com')
  
  db.session.add(u)
  </code></pre>
  
  <p>When I want to commit as</p>
  
  <pre><code>db.session.commit()
  </code></pre>
  
  <p>I get this error: </p>
  
  <blockquote>
    <p>cursor.execute(statement, parameters) sqlalchemy.exc.DatabaseError:
    (cx_Oracle.DatabaseError) ORA-02289: sequence does  not exist  [SQL:
    b'INSERT INTO "user" (id, nickname, email) VALUES (u_id.nextval,
    :nicknam e, :email) RETURNING "user".id INTO :ret_0'] [parameters:
    {b'email': 'john@email .com', b'ret_0': None, b'nickname': 'john',
    'ret_0': }]</p>
  </blockquote>
  
  <p>So basically it is saying that the sequence does not exist. I thought that sqlalchemy would actually create sequence and use the autoincrementing value as the unique id. Looks like that doesn't happen. So I start afresh and this time, I create the u_id sequence on the database and then issued the</p>
  
  <pre><code>db.session.commit()
  </code></pre>
  
  <p>again. When I do that I get this error message:</p>
  
  <blockquote>
    <p>sqlalchemy.orm.exc.FlushError: Instance  has a
    NULL iden tity key.  If this is an auto-generated value, check that
    the database table all ows generation of new primary key values, and
    that the mapped Column object is c onfigured to expect these generated
    values.  Ensure also that this flush() is no t occurring at an
    inappropriate time, such aswithin a load() event.</p>
  </blockquote>
  
  <p>I tried did this tutorial with sqlite and I didn't have to deal with autoincrement. I'm using Oracle now because that is the db I'll be developing against. Please help if you can. Thanks.</p>