HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlsqlalchemyMinor

How to set a auto-increment value in SQLAlchemy?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
incrementautovaluesqlalchemyhowset

Problem

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?

Solution

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)
)

Code Snippets

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)
)

Context

StackExchange Database Administrators Q#207812, answer score: 3

Revisions (0)

No revisions yet.