Skip to content

Custom primary key

By default, ORMagic will create a primary key column named id for each model. However, you can specify a custom primary key by setting the primary_key attribute on the model field to True.

from ormagic import DBModel, DBField

class User(DBModel):
    custom_id: int = DBField(primary_key=True)
    name: str

User.create_table()
CREATE TABLE IF NOT EXISTS user (
    custom_id INTEGER PRIMARY KEY,
    name TEXT NOT NULL
);