Create table in Oracle with auto-increment field using Sqlalchemy

旧时模样 提交于 2020-07-23 17:35:51

问题


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

来源:https://stackoverflow.com/questions/44021162/create-table-in-oracle-with-auto-increment-field-using-sqlalchemy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!