Schema Creation

Here we create connection to SQLite database client and then we discover & initialize models.

async classmethod Tortoise.generate_schemas(safe=True)[source]

Generate schemas according to models provided to .init() method. Will fail if schemas already exists, so it’s not recommended to be used as part of application workflow

Parameters:
safe=True

When set to true, creates the table only when it does not already exist.

Raises:

ConfigurationError – When .init() has not been called.

Return type:

None

generate_schema generates schema on empty database. There is also the default option when generating the schemas to set the safe parameter to True which will only insert the tables if they don’t already exist.

Helper Functions

async tortoise.utils.generate_schema_for_client(client, safe)[source]

Generates and applies the SQL schema directly to the given client.

Parameters:
client

The DB client to generate Schema SQL for

safe

When set to true, creates the table only when it does not already exist.

Return type:

None

tortoise.utils.get_schema_sql(client, safe)[source]

Generates the SQL schema for the given client.

Parameters:
client

The DB client to generate Schema SQL for

safe

When set to true, creates the table only when it does not already exist.

Return type:

str