Snowflake SQLAlchemy supports two ways to handle auto-incrementing values:
Sequence (Recommended for ORM): Uses a Sequence object. SQLAlchemy fetches the next value via a separate SELECT <seq>.nextval before the INSERT. This ensures the value is available to the ORM immediately after insert.AUTOINCREMENT: Uses SQLAlchemy's autoincrement=True. Snowflake assigns the value server-side during a single INSERT statement. This is faster but the value is not returned to the ORM in the same step.
Note: For Hybrid tables, both methods work, but Sequence is preferred for ORM-mapped classes.
from sqlalchemy import Table, Column, Integer, String, MetaData, Sequence
metadata = MetaData()
# Using Sequence (Recommended for ORM)
t = Table('mytable', metadata,
Column('id', Integer, Sequence('id_seq'), primary_key=True),
Column('name', String),
)
# Using AUTOINCREMENT (Single-statement insert)
t_auto = Table('mytable_auto', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('name', String),
)