Error in db.session.commit () with sqlachemy and Oracle

I am following the Mask Tutorial by Flask Mega here http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database with a little modification since instead of Oracle instead of sqlite it is used as db, I also use flask migrate instead of SQLAlchemy-migrate.

These are my models.py

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 '<User %r>' % (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 '<Post %r>' % (self.body)

      

According to the sqlachemy docs http://docs.sqlalchemy.org/en/latest/dialects/oracle.html , since oracle doesn't support autoincrement, I need to specify a sequence. I did this in the id column definition.

In the python command line, I can do:

from app import db, models

u = models.User(nickname='john', email='john@email.com')

db.session.add(u)

      

When I want to commit as

db.session.commit()

      

I am getting this error:

cursor.execute (operator, 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) RETURN "user" .id INTO: ret_0 '] [parameters: {b'email': 'john @ email.com', b'ret_0 ': None, b'nickname': 'john', 'ret_0':}]

So basically this is saying the sequence doesn't exist. I thought sqlalchemy would actually create a sequence and use the autoincrement value as a unique identifier. It doesn't seem to be happening. So I start over and this time I create a u_id sequence in the database and then it gives

db.session.commit()

      

again. When I do this, I get the following error message:

sqlalchemy.orm.exc.FlushError: The instance has a NULL identity key. If this is an auto-generated value, make sure the database table should generate new primary key values ​​and that the Column displayable is on in order to expect these generated values. Also make sure that this flash () does not have a t value that occurs at the wrong time, such as a load () event.

I tried to do this tutorial using sqlite and I didn't have to deal with auto-increments. I am using Oracle now because this is the db I will be developing with. Please help if you can. Thank.

+3


source to share


1 answer


It was a bug with cx_oracle 5.2, working with python 3.4.3. When I dropped to version 5.1.3, everything worked fine.



0


source







All Articles