4

This is my code :

    class csvimportt(Base):
     __tablename__ = 'csvimportt'

      #id = Column(INTEGER, primary_key=True, autoincrement=True)
      aid = Column(INTEGER(unsigned=True, zerofill=True),
          Sequence('article_aid_seq', start=1001, increment=1),
          primary_key=True)

I want to set a autoincrement value from 1000. How do I do it?

0

2 Answers 2

3

Starting from version 1.4, SQLAlechemy has Identity column.

Example copied from documentation:

from sqlalchemy import Table, Column, MetaData, Integer, Identity

metadata = MetaData()

data = Table(
    "data",
    metadata,
    Column(
        'id', Integer, Identity(start=42, cycle=True), primary_key=True
    ),
    Column('data', String)
)
2

From the SQLAlchemy 1.2 Documentation:

Postgresql 10 has a new IDENTITY feature that supersedes the use of SERIAL. Built-in support for rendering of IDENTITY is not available yet, however the following compilation hook may be used to replace occurrences of SERIAL with IDENTITY:

from sqlalchemy.schema import CreateColumn
from sqlalchemy.ext.compiler import compiles


@compiles(CreateColumn, 'postgresql')
def use_identity(element, compiler, **kw):
    text = compiler.visit_create_column(element, **kw)
    text = text.replace("SERIAL", "INT GENERATED BY DEFAULT AS IDENTITY")
    return text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.