Creating and using database tables within Baseten

Intact with Baseten Postgres tables.

You can create your own database tables and have Baseten manage both the database backing it and the necessary DB migrations as you create and update your table schemas. You can then use your database tables in your applications.

pageCreating tables

The database tables that you create here are available in your Code block and Decision block as SQLAlchemy ORM classes that you can use to query existing objects and create new ones. Use the context parameter to get

  • the SQLAlchemy ORM class representing your database table (context.classes)

  • the SQLAlchemy session object (context.session)

Limits

  • Operations on Baseten-backed tables are limited to 15 seconds

Usage

Here’s an example that shows how to use these constructs. To setup your block to access this table in your Code block, do the following:

def do(block_input, env, context):
    # All database table ORM classes are available under context.classes
    RestaurantPhotoLabel = context.classes.RestaurantPhotoLabel
    # And the SQLAlchemy session object is here:
    session = context.session

Note: All code examples that follow will reference the block_input, session and RestaurantPhotoLabel variables from here!

Once you have a database table created, you can now perform actions such as querying, creating, updating, and deleting objects of that table!

Last updated