0

I am creating a table in my Oracle DB using python and sqlalchemy. I would like to have an auto-increment ID column as primary key. How can I modify the following code to add the autoload option or anything like that ?

engine = creat_engine("oracle:// ....")
Base = declarative_base()

class MyTable(Base):
    __tablename__ = 'MyTable'
    ID = Column(Integer, Sequence('my_id_seq'),  primary_key=True)
    SomeColumn = Column(VARCHAR(50))

Base.metadata.create_all(engine)

p.s. I don't want to create a separate sequence and trigger (as shown here).

UPDATE:

when trying to do the following, I get syntax error because of "autoload=True":

class MyTable(Base):
    __table__ = Table('myTable', Base.metadata, autoload=True,
        Column('id', Integer, Sequence('my_id_seq'), primary_key=True),
        Column('somecolumn', VARCHAR(50))
)

SyntaxError: non-keyword arg after keyword arg

Community
  • 1
  • 1
Itack
  • 222
  • 1
  • 3
  • 13
  • Oracle 12c introduced `IDENTITY` columns. Since it may be relevant to the answers, can you tag your question with the Oracle version you are using? – MT0 May 17 '17 at 09:46
  • @MT0 actually we are using the code on two environments, one with Oracle 11 and one with 12. so I need something that is compatible to both versions. but in case of 12c, can you tell me where to put this identity in my code ? thanks. – Itack May 17 '17 at 11:33
  • A quick internet search for "oracle identity column" turns up many results including the page you have linked in your question. – MT0 May 17 '17 at 12:07
  • Your syntax error is trivially solved by putting the `autoload=True` **after** the `Column(...)` definitions. – Martijn Pieters Aug 05 '19 at 13:35

0 Answers0