I am leveraging Tiangolo's SqlModel library for a FastAPI + PostgreSQL application.
As best as I can tell, primary keys are defined as follows:
class SomeSchema(SqlModel, table=True):
__tablename__ = 'some_schema'
id: Optional[int] = Field(default=None, primary_key=True)
Using this pattern, I receive SQL Alchemy warnings:
SAWarning: Column 'some_schema.id' is marked as a member of the primary key for table 'some_schema', but has no Python-side or server-side default generator indicated, nor does it indicate 'autoincrement=True' or 'nullable=True', and no explicit value is passed. Primary key columns typically may not store NULL. Note that as of SQLAlchemy 1.1, 'autoincrement=True' must be indicated explicitly for composite (e.g. multicolumn) primary keys if AUTO_INCREMENT/SERIAL/IDENTITY behavior is expected for one of the columns in the primary key. CREATE TABLE statements are impacted by this change as well on most backends.
Unlike sqlalchemy Column objects, the sqlmodel Field object has no autoincrement
option. The sqlmodel documentation says nothing about auto-incrementing ID columns.
What is the best practice for defining an id
column as an auto-incrementing primary key using this library?