Defining a Schema

The schema is usually defined within the tables.py file of your Piccolo app.

This reflects the tables in your database. Each table consists of several columns. Here’s a very simple schema:

# tables.py
from piccolo.table import Table
from piccolo.columns import Varchar


class Band(Table):
    name = Varchar(length=100)

For a full list of columns, see column types.

Hint

If you’re using an existing database, see Piccolo’s auto schema generation command, which will save you some time.


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


Tablename

By default, the name of the table in the database is the Python class name, converted to snakecase. For example Band -> band, and MusicAward -> music_award.

You can specify a custom tablename to use instead.

class Band(Table, tablename="music_band"):
    name = Varchar(length=100)

Connecting to the database

In order to create the table and query the database, you need to provide Piccolo with your connection details. See Engines.