Primary Key¶
UUID¶
To specify a UUID primary key
column, do the following:
# tables.py
from piccolo.table import Table
from piccolo.columns import UUID, Varchar
class Band(Table):
id = UUID(primary_key=True)
name = Varchar(length=100)
Identity¶
To specify an identity column as the primary key, do the following:
# tables.py
from piccolo.table import Table
from piccolo.columns import BigInt, Varchar
from piccolo.columns.generated import Identity
class Band(Table):
id = BigInt(primary_key=True, generated=Identity())
name = Varchar(length=100)
Hint
This is the recommended alternative to Serial in Postgres.
Serial / BigSerial¶
To specify a Serial /
BigSerial column as the
primary key, do the following:
# tables.py
from piccolo.table import Table
from piccolo.columns import BigSerial, Varchar
class Band(Table):
id = BigSerial(primary_key=True)
name = Varchar(length=100)
Hint
Even though identity columns are recommended instead of
Serial / BigSerial for Postgres, these still work fine - the differences are
fairly minor.
Default¶
If no primary key is specified, then Piccolo tables are automatically given one
called id, which is an auto incrementing integer
(a Serial(primary_key=True) column).